[IronPython] IP 2.0 Hosting

Slide slide.o.mix at gmail.com
Mon Dec 17 21:38:01 CET 2007


On Dec 17, 2007 9:48 AM, Dino Viehland <dinov at exchange.microsoft.com> wrote:
> Yes, the document is not yet up to date - we're currently working on implementing it so the next release will probably have some of it, and subsequence releases will have the whole thing.
>
> For giving objects to C# code you need to derive from the interface (and implement the methods).  If you fail to implement any methods we'll throw at runtime when that method is called.
>
> For exposing classes/namespaces/etc...  I think we may have a deficiency here even in the new model.  While we do have a global namespace per ScriptRuntime I don't believe we have a way to say "populate this with these types/namespaces".  I'll bring this up next time we're going over the spec internally.
>
> Here's some simple code you can give a shot to get started.  I don't have anything comprehensive, this is just what I pulled from EngineTest.cs which if you get the IronPython source code is in IronPythonTest.  I imagine whatever Toni can post will be more useful :).
>
> using IronPython.Runtime.Hosting;
> using Microsoft.Scripting;
> ...
>
> PythonEngine pe = PythonEngine.CurrentEngine;
>
> pe.Execute(script); # where script is a string w/ some code in it.  This runs in the DefaultModule which is:
>
> IScriptModule default_module = ScriptDomainManager.CurrentManager.Host.DefaultModule;
>
> You can also do:
>
> pe.Execute("x = 1", anonymousModule);
>
> You can create new modules via:
>
> ScriptModule publishedModule = pe.CreateModule("published_context_test", ModuleOptions.PublishModule);
>
>


Can you give some quick examples of how to import modules from C#? I
am having issues with import exceptions when I try to do things. The
path to the module is definitely within the sys.path, but I get all
sorts of errors. Here is a snippet of my code:

_script_engine.AddToPath(Path.GetDirectoryName(_scriptPath));
_script_engine.AddToPath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
_script_engine.AddToPath(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"Lib"));

foreach (string assembly in _assemblies)
      _script_engine.SystemState.ClrModule.AddReferenceByPartialName(Path.GetFileNameWithoutExtension(assembly));

foreach (KeyValuePair<string, object> obj in _objects)
      __main__.SetVariable(obj.Key, obj.Value);

_script_engine.SystemState.prefix =
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

_script_engine.Execute("import ScriptHelper");
_script_engine.Execute(string.Format("import {0} as Config",
Path.GetFileNameWithoutExtension(_scriptPath)));


ScriptHelper.py is just a set of simple python functions for grabbing
information about loaded modules, etc. It resides in the current
working directory (same as the EntryAssembly.Location). My problem
comes from something inside the script (which is written to a temp
file by the UI in my simple test app) importing something (which has
been added using the ClrModule.AddReferenceByPartialName). I get a
'Script' is not defined (with Script being a DLL in the working
directory which has been added as a reference).

THanks



More information about the Ironpython-users mailing list