The 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-boun...@lists.ironpython.com [mailto:users- > boun...@lists.ironpython.com] On Behalf Of jhow...@drawloop.com > Sent: Tuesday, November 17, 2009 2:31 PM > To: users@lists.ironpython.com > 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...@drawloop.com" <jhow...@drawloop.com> > 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...@microsoft.com> 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...@lists.ironpython.com [mailto:users- > > > > boun...@lists.ironpython.com] On Behalf Of jhow...@drawloop.com > > > > Sent: Tuesday, November 17, 2009 2:02 PM > > > > To: us...@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...@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...@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...@lists.ironpython.com > > > > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > > > > _______________________________________________ > > > > > Users mailing list > > > > > > > us...@lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/user > > > > s-ironpython.com > > > > _______________________________________________ > > > > Users mailing list > > > > us...@lists.ironpython.com > > > >http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > > _______________________________________________ > > > Users mailing list > > > > us...@lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/user > s-ironpython.com > > > > _______________________________________________ > > Users mailing list > > > us...@lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/user > s-ironpython.com > _______________________________________________ > Users mailing list > Users@lists.ironpython.com > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com _______________________________________________ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com