[IronPython] IronPython 2: Oddity with Hosting API from within IronPython

Curt Hagenlocher curt at hagenlocher.org
Fri Oct 31 15:03:52 CET 2008


I don't know the answer to your question (and don't have the source code
with me on the bus) but by calling Python.CreateEngine(), you're now
creating a second copy of the runtime.  Notionally, the runtimes are
independent of each other and I would expect there to be a second import
when the same module is used in both.

On Fri, Oct 31, 2008 at 6:53 AM, Michael Foord <
michael.foord at resolversystems.com> wrote:

> Hello guys,
>
> In Resolver One we use the IronPython hosting API from inside IronPython
> code. I've noticed an oddity that is not how I would expect the hosting API
> to behave if I was using it from C#.
>
> My understanding is that the correct way to publish a module (make it
> available for a ScriptEngine to import) is to set it in
> 'engine.Runtime.Globals'.
>
> If I do this from within IronPython code with a module I have already
> imported and then execute an import statement in the engine, the module is
> re-imported (code executed) rather than using the one I have published to
> the runtime globals.
>
> If I have a 'foobar' module that prints when importing, the following code
> prints twice instead of the once I would expect:
>
> import sys
> import clr
> clr.AddReference('IronPython')
> clr.AddReference('Microsoft.Scripting')
>
> from IronPython.Hosting import Python
> from Microsoft.Scripting import SourceCodeKind
>
> import foobar
>
> engine = Python.CreateEngine()
> engine.Runtime.Globals.SetVariable('foobar', sys.modules['foobar'])
>
> source = engine.CreateScriptSourceFromString('import foobar\r\n',
> SourceCodeKind.Statements)
> scope = engine.CreateScope()
> source.Compile().Execute(scope)
>
>
> *However*, if I change the code to not use Runtime.Globals, but instead do
> the following, then the module is only imported once and I get one print as
> expected:
>
> hostedSys = Python.GetSysModule(engine)
> hostedSys.modules['foobar'] = sys.modules['foobar']
>
> Is there something I have overlooked here?
>
> As a minor supplementary question, how do I get a reference to the default
> ScriptScope on an engine? Is there any performance advantage in using the
> default one, can I replace it, and does replacing it remove any performance
> benefits we might have got? (OK, so strictly speaking that wasn't just one
> question...)
>
> All the best,
>
> Michael Foord
>
> --
> Michael Foord
> Senior Software Engineer, Resolver Systems Ltd.
> michael.foord at resolversystems.com
> +44 (0) 20 7253 6372
>
> Try out Resolver One! <http://www.resolversystems.com/get-it/>
>
> 17a Clerkenwell Road, London EC1M 5RD, UK
> VAT No.: GB 893 5643 79 Registered in England and Wales as company number
> 5467329.
> Registered address: 843 Finchley Road, London NW11 8NA, UK
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081031/aa6be340/attachment.html>


More information about the Ironpython-users mailing list