[IronPython] creating a scope for embedded scripts to run in

Toni Alatalo antont at kyperjokki.fi
Tue Nov 13 15:09:26 CET 2007


On Tue, 13 Nov 2007, Fuzzyman wrote:

> I don't have the API docs to hand - but what you want to do is to
> effectively execute an import statement in your namespace.

ah - there indeed is an import call in the API - i just always assumed 
that it works like import in the scripts, meaning that it can only import 
modules that have already been exposed to the scripts somehow.

but i'll test that next.

btw, from ironpython exposing classes seems to work just fine, so porting 
would indeed solve this:

IronPython console: IronPython 2.0A6 (2.0.11102.00) on .NET 2.0.50727.312
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import clr
>>> clr.AddReference("IronPython.dll")
>>> clr.AddReference("Microsoft.Scripting.dll")
>>> import IronPython
>>> import Microsoft.Scripting
>>> py = IronPython.Hosting.PythonEngine.CurrentEngine;
>>> import System.Threading
>>> from Microsoft.Scripting import Script
>>> Script.SetVariable("Timer", System.Threading.Timer)
>>> Script.Execute("python", "print Timer")
<type 'Timer'>
>>> def p(s):
...     print s
...
>>> p('hep')
hep
>>> Script.SetVariable('p', p)
>>> Script.Execute("python", "p('hep')")
hep
>>> Script.Execute("python", "t = Timer(lambda x: p('tick')); 
t.Change(2000, 0)"
)
>>> tick

.. that last tick came from the worker thread 2s later, as i intended :)
now off to test how the imports work.

~Toni



More information about the Ironpython-users mailing list