[IronPython] Global and Local ScriptScope

Caspar Lessing caspar.lessing at gmail.com
Tue Jan 6 14:58:32 CET 2009


Hello People

I use an embedded instance of IronPython.
My program spends effort to create a global context. It then continues to to
execute statements inside a local context which need resolve the items in
the global context as well. Think of python functions' local context and its
interaction with the module context.

In an older version of IronPython I had a EngineModule which I used for my
global context and I could use a Dictionary<string,object> for my locals.

This allowed my to do the following:

PythonEngine pe = new PythonEngine();
Context = pe.CreateModule();
pe.Execute(ExpensiveGlobalSetupScript,Context);
Dictionary<string,object>[] locals = new
Dictionary<string,object>[ScriptList.Length];
for(int i = 0; i < ScriptList.Length; i++)
{
  locals[i] = new Dictionary<string,object>()
  pe.Execute(ScriptList[i], Context, locals[i]);
}

I am having trouble doing something similar with ScriptScope. I have
explored some avenues:
1. Copying the global context into each local one. It seems too expensive.
Possibly it is only the cloning of some form of dictionary, but still.
2. Implementing a new CustomSymbolDictionary and overriding
TryGetExtraValue. Problem is that it is called before trying to resolve
symbols internally (Which leads to globals being resolved rather than
locals)
3. Creating my own implementation of IAttributesCollection (Seemed too
complex after discovering the Parent mechanism in Scope)

I eventually found the Parent mechanism inside Scope. However I do not have
access to create a new ScriptScope (Internal constructor) based on a Parent
Scope.
I had to create a new Factory method inside ScriptEngine which looks as
follows:

    public sealed class ScriptEngine
    {
      .....
        public ScriptScope CreateScope(ScriptScope parent)
        {
          ContractUtils.RequiresNotNull(parent, "parent");
          return new ScriptScope(this, new Scope(parent.Scope,null));
        }
      ....
     }

My question if whether this is a sensible addition to ScriptEngine or am I
missing something?

Much appreciated
Caz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090106/ba7415ed/attachment.html>


More information about the Ironpython-users mailing list