[IronPython] hosting and SetVariable

Slide slide.o.mix at gmail.com
Thu May 29 18:32:23 CEST 2008


Ok, one more question and I should be good to go. I used to call
Microsoft.Scripting.ClrModule.GetInstance().AddReference to add
references from my hosting code. With the latest beta, I don't see
that anywhere and was wondering how to do this.

thanks

On Thu, May 29, 2008 at 8:42 AM, Slide <slide.o.mix at gmail.com> wrote:
> Apparently I need to update to the latest IP2.0 beta. I don't even see
> CurrentManager.Globals, thanks for the info.
>
> On Thu, May 29, 2008 at 8:37 AM, Dino Viehland
> <dinov at exchange.microsoft.com> wrote:
>> What you want to do is put the variables in ScriptDomainManager.Globals/ScriptRuntime.Globals and then you can import them.  For example:
>>
>> ScriptDomainManager.CurrentManager.Globals.SetVariable("hello_world", "hi!");
>>
>> Then you can do:
>>
>> import hello_world
>>
>> and we'll pick it up from globals.  Just FYI IronPython.Hosting.PythonEngine is going away (along w/ CurrentEngine) so you're better off creating a ScriptRuntime and getting a ScriptEngine from it by name.
>>
>> -----Original Message-----
>> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Slide
>> Sent: Thursday, May 29, 2008 8:14 AM
>> To: Discussion of IronPython
>> Subject: [IronPython] hosting and SetVariable
>>
>> Perhaps this is just missing knowledge on my part, but here goes.
>>
>> I am using IP2.0 with the hosting API to add scripting support to my
>> application. I use the following code to create my main module from
>> the script file the user selects:
>>
>>  ICompiledCode code =
>> IronPython.Hosting.PythonEngine.CurrentEngine.Compile(SourceUnit.CreateFileUnit(_context,
>> path), _error_sink);
>> __main__ = code.MakeModule("__main__") as ScriptScope;
>>
>> // create our module.
>> ScriptDomainManager.CurrentManager.PublishModule(__main__, "__main__");
>>
>> // add all the references the "Host" wanted.
>> foreach (string assembly in _assemblies)
>>      Microsoft.Scripting.ClrModule.GetInstance().AddReference(Path.GetFileNameWithoutExtension(assembly));
>>
>> // set all the variables that the "Host" wanted set.
>> foreach (KeyValuePair<string, object> obj in _objects)
>>      __main__.SetVariable(obj.Key, obj.Value);
>>
>> The last foreach is setting up my object model which is available to
>> my scripts. My problem arises when I want to use one of these
>> variables from a script that is imported from the user selected script
>> file.
>>
>> For instance, if I add an object _objects.Add("MyObj", someObj), I can
>> reference that object in the user script as MyObj with no problem.
>> What I would like to do is have these variables global to all scripts
>> that are imported, etc. I tried import __main__ in my other scripts,
>> but __main__ doesn't show up in sys.modules (which is what I thought
>> PublishModule would do, but thats a different story).
>>
>> Am I going about this the wrong way?
>>
>> Thanks
>>
>> slide
>>
>> --
>> slide-o-blog
>> http://slide-o-blog.blogspot.com/
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>
>
>
> --
> slide-o-blog
> http://slide-o-blog.blogspot.com/
>



-- 
slide-o-blog
http://slide-o-blog.blogspot.com/



More information about the Ironpython-users mailing list