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

Toni Alatalo antont at kyperjokki.fi
Tue Nov 13 14:50:03 CET 2007


Hi,

I've been struggling a lot with trying to create a scope / namespace / 
environment / something for embedded scripts to run in.

Putting in instances with (in 2.0) SetVariable is fine, but so far I've 
not managed to put classes there (I wish classes were objects like in 
Python so things would just work .. in fact I'm considering porting this 
script server thing other people have earlier started as c# to ironpy to 
perhaps get rid of these probs :)

So I hope there is either something nice in the new Microsoft.Scripting 
API that I've not found yet, or something in the .net reflection system 
that am overlooking. Have tried this kind of things:

System.Type Timer = new 
System.Threading.Timer((System.Threading.TimerCallback)this.DummyTimerCallback).GetType();

Script.SetVariable("Timer", Timer);

.. but of course that just puts the (useless?) reflection class to the 
namespace, not the actual class that could be constructed? Perhaps that 
typeinfo-type can be used to instanciate with some Invoke thing, but 
obviously I don't want to expose that to scripters. Dunno if I could hide 
that in some wrapper funcs implemented on the py side.

Previously I've used this kind of technique to expose some modules for 
completely trusted scripts: (this from 1.1, in a project where we used 
Mogre - we are not using it in this project)

Mogre.Timer t = new Mogre.Timer(); //just to get the mogre module
python.LoadAssembly(t.GetType().Assembly); //gives 'Mogre'

so after that in 1.1 'import Mogre' worked in the scripts and that was 
fine there.

but now I'd like to give the scripts Threading.Timer, but not the whole 
System assembly.

I've seen the CreateModule etc. methods in ipy, and also now the new 
CreateScope thing in Microsoft.Scripting, but did not find any 
documentation on that - and anyway the same problem seems to be there that 
I can't put references to classes as values in the scopes.

Should I indeed try porting the whole thing to IronPython to perhaps get 
rid of this prob., or is there some nice way to expose assemblies or parts 
of them? Sorry if this is FAQ stuff but I did not find this in any of the 
embedding examples etc.

patiently looking forward to be shown the right path,
~Toni



More information about the Ironpython-users mailing list