[IronPython] Embedded IronPython 2.6 Module Name

Dino Viehland dinov at microsoft.com
Tue Nov 17 23:12:16 CET 2009


I think you now want to do:

	      PythonModule pm = new PythonModule();
            ScriptEngine se = Python.CreateEngine();
            PythonContext pc = (PythonContext) HostingHelpers.GetLanguageContext(se);
            pc.PublishModule("__main__", pm);
	    
  	      var modContext = new ModuleContext(pm, pc);
            
            ScriptScope ss = HostingHelpers.CreateScriptScope(se, modContext.GlobalScope);
            ss.SetVariable("__name__", "__ main__");
            ss.SetVariable("__doc__", "");


The change here is to create a ModuleContext which will let you then get the Scope.

I agree this has gotten worse in 2.6 - I opened a bug a while ago to make working with
modules easier - http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=25190.


> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of jhoward at drawloop.com
> Sent: Tuesday, November 17, 2009 2:02 PM
> To: users at lists.ironpython.com
> Subject: Re: [IronPython] Embedded IronPython 2.6 Module Name
> 
> I realize I'm replying rather late, but I just got to trying this
> again.  This is something that really should be simple.  Anytime a
> module is run from the ScriptEngine directly, I would expect the
> behavior to be running as "__main__" just as if I was running it from
> the command line using "ipy" or "python".  Unfortunately, trying to
> create a module directly doesn't work as far as naming the module.
> Using the following code:
> 
>             PythonModule pm = new PythonModule();
>             ScriptEngine se = Python.CreateEngine();
>             PythonContext pc = (PythonContext)
> HostingHelpers.GetLanguageContext(se);
>             pc.PublishModule("__main__", pm);
>             ScriptScope ss = HostingHelpers.CreateScriptScope(se, new
> Microsoft.Scripting.Runtime.Scope(pm.Get__dict__()));
>             ss.SetVariable("__name__", "__main__");
>             ss.SetVariable("__doc__", "");
> 
> doesn't work.  There's no way to directly get the Scope from the
> PythonModule when working this way, as it's been marked as internal.
> Looking through the debugger, the _scope variable that actually holds
> the scope on the PythonModule object is null.  I believe the old
> CreateModule way of doing this would have worked, but there's no way
> to that I've found to do this now.
> 
> At this point, I'm really not sure how 2.6 is being marked as a
> release candidate.
> 
> On an unrelated note, I could, in IronPython 1.1.2 do the following
> code:
> 
>             _pyEngine.Execute("python code", _pyEngine.DefaultModule,
> args);
> 
> where "args" is a Dictionary<string, object> and have those arguments
> passed in to a function call or the like.  Is there any way to do this
> using the new hosting engine?
> 
> Thanks again.
> 
> 
> On Nov 6, 2:18 pm, Curt Hagenlocher <c... at hagenlocher.org> wrote:
> > It looks like you can just create the PythonModule directly now --
> it's a
> > public class with a public constructor.
> >
> > On Thu, Nov 5, 2009 at 12:14 PM, Jonathan Howard
> <jhow... at drawloop.com>wrote:
> >
> > > Thanks for the help, Curt.  Perhaps it's a problem with the latest,
> RC?
> > >  There is no "CreateModule" function on the PythonContext object.
> >
> > > ~Jonathan
> >
> > > _______________________________________________
> > > Users mailing list
> > > Us... at lists.ironpython.com
> > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >
> >
> >
> > _______________________________________________
> > Users mailing list
> >
> Us... at lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/user
> s-ironpython.com
> _______________________________________________
> 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