[IronPython] Two questions regarding exposing an API.

Dan Eloff dan.eloff at gmail.com
Tue Jul 8 17:52:03 CEST 2008


Sorry, missed the unexpected token '\r' part. The error pretty much
tells you what the problem is, you've got a \r (probably because you
have \r\n line endings, in the the variable moduleSource. You can try
to eliminate those, but you can also just do exec
moduleSource.replace('\r', '') in newModule.__dict__

-Dan

On Tue, Jul 8, 2008 at 7:22 AM, TraumaPony <pontheangelofdeath at gmail.com> wrote:
> Ah, true, however, I still get the same error.
>
> 2008/7/8 Dan Eloff <dan.eloff at gmail.com>:
>>
>> Hi,
>>
>> exec is not a function, it's a statement.
>>
>> exec moduleSource in newModule.__dict__
>>
>> -Dan
>>
>> On Tue, Jul 8, 2008 at 6:40 AM, TraumaPony <pontheangelofdeath at gmail.com>
>> wrote:
>> > Hi, I have two questions regarding the IronPython 2.0 B3.
>> >
>> > 1. I'm trying to expose only a few methods from my assembly, but I
>> > haven't
>> > the faintest idea how to do it. The best I can come up with is the
>> > following:
>> >
>> > import clr
>> > clr.AddReference("CoolEngine")
>> > def randomIntNoParams():
>> >     from CoolEngine.Engine.Scripting import ScriptManager
>> >     return CoolEngine.Engine.Scripting.ScriptManager.RandomInt()
>> > def randomIntOneParam(i):
>> >     from CoolEngine.Engine.Scripting import ScriptManager
>> >     return CoolEngine.Engine.Scripting.ScriptManager.RandomInt(i)
>> > def randomIntTwoParams(i, j):
>> >     from CoolEngine.Engine.Scripting import ScriptManager
>> >     return CoolEngine.Engine.Scripting.ScriptManager.RandomInt(i, j)
>> > def randomIntThreeParams(i, j, k):
>> >     from CoolEngine.Engine.Scripting import ScriptManager
>> >     return CoolEngine.Engine.Scripting.ScriptManager.RandomInt(i, j, k)
>> > def randomIntThreeParamsNoReturn(i, j, k):
>> >     from CoolEngine.Engine.Scripting import ScriptManager
>> >     CoolEngine.Engine.Scripting.ScriptManager.RandomIntNoReturn(i, j, k)
>> > But alas, it says "name 'CoolEngine' is not defined" . What am I doing
>> > wrong
>> > there?
>> >
>> > 2. I'm trying to create a module dynamically at runtime. I have the
>> > following code which I want to be placed in a module called "Script":
>> >
>> > import clr
>> > clr.AddReference("System.Threading")
>> > def Pause(milliseconds):
>> >     from System.Threading.Tasks import Task
>> >     Task.CurrentTask.Wait(milliseconds)
>> >
>> > However, I have no idea how to get it into its own module from there,
>> > and
>> > including it in its own file called Script.py is out of the question; I
>> > have
>> > that code as a string resource in my assembly.
>> >
>> > I'm doing the following:
>> >
>> > Engine.CreateScriptSourceFromString(
>> >
>> > "import imp\n"
>> >
>> > +
>> >
>> > "import sys\n"
>> >
>> > +
>> >
>> > "newModule = imp.new_module(moduleName)\n"
>> >
>> > +
>> >
>> > "exec(moduleSource, newModule.__dict__)\n"
>> >
>> > +
>> >
>> > "sys.modules[moduleName] = newModule"
>> >
>> > , SourceCodeKind.Statements).Execute(scope);
>> >
>> > where the moduleName variable is "Script" and the moduleSource is the
>> > above
>> > source code. However, I get the following message:
>> >
>> > An unhandled exception of type
>> > 'Microsoft.Scripting.SyntaxErrorException'
>> > occurred in Unknown Module.
>> >
>> > Additional information: unexpected token '\r'
>> >
>> > Any ideas?
>> >
>> >
>> >
>> > --
>> > /"\
>> > \ / ASCII RIBBON CAMPAIGN
>> > X AGAINST HTML MAIL
>> > / \
>> >
>> > This message has been encrypted with ROT-26. Any unauthorised decryption
>> > will result in prosecution to the full extent of the law.
>> >
>> > _______________________________________________
>> > Users mailing list
>> > Users at lists.ironpython.com
>> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>> >
>> >
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
> --
> /"\
> \ / ASCII RIBBON CAMPAIGN
> X AGAINST HTML MAIL
> / \
>
> This message has been encrypted with ROT-26. Any unauthorised decryption
> will result in prosecution to the full extent of the law.
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>



More information about the Ironpython-users mailing list