|
View:
New views
10 Messages
—
Rating Filter:
Alert me
|
|
|
Embedded IronPython 2.6 Module NameHi,
We're trying to upgrade to IronPython 2.6 and are having a number of issues with it. The biggest issue is there doesn't appear to be a way to name the module that runs when you embed your code. We're using the Hosting API to create and run code within C#. The code runs, but we have other modules that are supposed to pull globals from the __main__ module. This worked in 1.1 by setting the DefaultModule on the PythonEngine instance. There doesn't seem to be a way to do this anymore.
Any help would be appreciated. ~Jonathan
_______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
|
|
|
Re: Embedded IronPython 2.6 Module NameI'm not sure if there's a simpler way -- here's what I did a few months back when I needed to publish something via __main__:
pythonContext = HostingHelpers.GetLanguageContext(self._engine)
module = pythonContext.CreateModule()
pythonContext.PublishModule('__main__', module) scope = HostingHelpers.CreateScriptScope(self._engine, module.Scope) scope.SetVariable('__name__', '__main__') scope.SetVariable('__doc__', '') HostingHelpers is a class in the DLR hosting interfaces. From C#, you'd need to explicitly cast the LanguageContext to a PythonContext. After this, you would run your Python code within the "scope" ScriptScope.
On Thu, Nov 5, 2009 at 10:57 AM, Jonathan Howard <jhoward@...> wrote: Hi, _______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
|
|
|
Re: Embedded IronPython 2.6 Module NameIt 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 <jhoward@...> wrote: Thanks for the help, Curt. Perhaps it's a problem with the latest, RC? There is no "CreateModule" function on the PythonContext object. _______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: Embedded IronPython 2.6 Module NameI 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...@...> 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...@...>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...@... > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > _______________________________________________ > Users mailing list > Us...@...://lists.ironpython.com/listinfo.cgi/users-ironpython.com Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: Embedded IronPython 2.6 Module NameI 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@... [mailto:users- > bounces@...] On Behalf Of jhoward@... > Sent: Tuesday, November 17, 2009 2:02 PM > To: users@... > 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...@...> 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...@...>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...@... > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > > > > > _______________________________________________ > > Users mailing list > > > Us...@...://lists.ironpython.com/listinfo.cgi/user > s-ironpython.com > _______________________________________________ > Users mailing list > Users@... > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: Embedded IronPython 2.6 Module NameThanks, that gives me at least something. Any idea why:
ss.Engine.Execute("__name__", ss); returns "<module>" but: ss.Engine.CreateScriptSourceFromString("__name__", SourceCodeKind.Expression).Execute(ss); returns "__main__"? On Nov 17, 2:12 pm, Dino Viehland <di...@...> wrote: > 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-boun...@... [mailto:users- > > boun...@...] On Behalf Of jhow...@... > > Sent: Tuesday, November 17, 2009 2:02 PM > > To: us...@... > > 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...@...> 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...@...>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...@... > > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > _______________________________________________ > > > Users mailing list > > > Us...@...://lists.ironpython.com/listinfo.cgi/user > > s-ironpython.com > > _______________________________________________ > > Users mailing list > > Us...@... > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > _______________________________________________ > Users mailing list > Us...@...://lists.ironpython.com/listinfo.cgi/users-ironpython.com Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: Embedded IronPython 2.6 Module NameMore specifically, there seems to be four easy ways to execute a
string of python code. The following code has the output listed below: Console.WriteLine(ss.Engine.CreateScriptSourceFromString ("__name__", SourceCodeKind.Expression).Execute(ss)); Console.WriteLine(ss.Engine.CreateScriptSourceFromString ("__name__", SourceCodeKind.Expression).Execute()); Console.WriteLine(ss.Engine.Execute("__name__")); Console.WriteLine(ss.Engine.Execute("__name__", ss)); Output: __main__ __builtin__ <module> <module> On Nov 17, 2:26 pm, "jhow...@..." <jhow...@...> wrote: > Thanks, that gives me at least something. Any idea why: > > ss.Engine.Execute("__name__", ss); > > returns "<module>" but: > > ss.Engine.CreateScriptSourceFromString("__name__", > SourceCodeKind.Expression).Execute(ss); > > returns "__main__"? > > On Nov 17, 2:12 pm, Dino Viehland <di...@...> wrote: > > > 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-boun...@... [mailto:users- > > > boun...@...] On Behalf Of jhow...@... > > > Sent: Tuesday, November 17, 2009 2:02 PM > > > To: us...@... > > > 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...@...> 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...@...>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...@... > > > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > > _______________________________________________ > > > > Users mailing list > > > > Us...@...://lists.ironpython.com/listinfo.cgi/user > > > s-ironpython.com > > > _______________________________________________ > > > Users mailing list > > > Us...@... > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > _______________________________________________ > > Users mailing list > > Us...@...://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > _______________________________________________ > Users mailing list > Us...@...://lists.ironpython.com/listinfo.cgi/users-ironpython.com Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: Embedded IronPython 2.6 Module NameThe logic for assigning these is definitely a little bit weird.
The theory is that we only assign __name__ if we believe the code being executed is a module. This is to mimic the behavior of exec/eval which doesn't set __name__: >>> x = {'__name__':'foo'} >>> exec 'print "hi"' in x hi >>> x['__name__'] 'foo' >>> So in the 1st two cases the theory is that we're not setting it all. I suspect in the 1st case though we are actually setting it somewhere when we create the default scope for executing the code. The last 2 we believe are modules but we don't have a path for the source code. When that happens we set the name to <module>. I believe this is an attempt to match some behavior of CPython but I can't quite figure out what behavior it is replicating right now. > -----Original Message----- > From: users-bounces@... [mailto:users- > bounces@...] On Behalf Of jhoward@... > Sent: Tuesday, November 17, 2009 2:31 PM > To: users@... > Subject: Re: [IronPython] Embedded IronPython 2.6 Module Name > > More specifically, there seems to be four easy ways to execute a > string of python code. The following code has the output listed > below: > > Console.WriteLine(ss.Engine.CreateScriptSourceFromString > ("__name__", SourceCodeKind.Expression).Execute(ss)); > Console.WriteLine(ss.Engine.CreateScriptSourceFromString > ("__name__", SourceCodeKind.Expression).Execute()); > Console.WriteLine(ss.Engine.Execute("__name__")); > Console.WriteLine(ss.Engine.Execute("__name__", ss)); > > Output: > > __main__ > __builtin__ > <module> > <module> > > On Nov 17, 2:26 pm, "jhow...@..." <jhow...@...> > wrote: > > Thanks, that gives me at least something. Any idea why: > > > > ss.Engine.Execute("__name__", ss); > > > > returns "<module>" but: > > > > ss.Engine.CreateScriptSourceFromString("__name__", > > SourceCodeKind.Expression).Execute(ss); > > > > returns "__main__"? > > > > On Nov 17, 2:12 pm, Dino Viehland <di...@...> wrote: > > > > > 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-boun...@... [mailto:users- > > > > boun...@...] On Behalf Of jhow...@... > > > > Sent: Tuesday, November 17, 2009 2:02 PM > > > > To: us...@... > > > > 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...@...> 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...@...>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...@... > > > > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > > > > _______________________________________________ > > > > > Users mailing list > > > > > > > Us...@...://lists.ironpython.com/listinfo.cgi/user > > > > s-ironpython.com > > > > _______________________________________________ > > > > Users mailing list > > > > Us...@... > > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > > _______________________________________________ > > > Users mailing list > > > > Us...@...://lists.ironpython.com/listinfo.cgi/user > s-ironpython.com > > > > _______________________________________________ > > Users mailing list > > > Us...@...://lists.ironpython.com/listinfo.cgi/user > s-ironpython.com > _______________________________________________ > Users mailing list > Users@... > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
| Free embeddable forum powered by Nabble | Forum Help |