[IronPython] How do you run compiled scripts(dll) by C# host?

KATO Kanryu k.kanryu at gmail.com
Tue Aug 18 07:10:59 CEST 2009


2009/8/18 Curt Hagenlocher <curt at hagenlocher.org>
> using Microsoft.Scripting.Hosting.Providers.HostingHelpers.
OK. but after imported not set data to the scope or the scriptscope.

| var scope = HostingHelpers.GetScope(scriptScope);
| var codeContext = new CodeContext(scope, pythonContext);
| Importer.Import(codeContext, main, new PythonTuple(), 0);
|
|: scope.Dict is empty



2009/8/18 Dino Viehland <dinov at microsoft.com>:
> What Curt says is true but I would suggest doing scriptRuntime.LoadAssembly
> instead.
> Once you do that you can do engine.ImportModule (an extension method defined
> in the Python class) do get a ScriptSource for any of your compiled
> modules.  InitializeModule may change from major version to major version of
> IronPython – but engine.ImportModule is a fixed public API.

Thanks!
I made it the following.

| public static void InitializeModule(string ModulePath, string/*!*/ main)
| {
|     var pythonEngine = Python.CreateEngine();
|     var path = Path.GetFullPath(ModulePath);
|     var precompiled = Assembly.LoadFile(path);
|
|     pythonEngine.Runtime.LoadAssembly(typeof(string).Assembly);
|     pythonEngine.Runtime.LoadAssembly(typeof(System.Diagnostics.Debug).Assembly);
|     pythonEngine.Runtime.LoadAssembly(precompiled);
|
|     var pythonContext =
HostingHelpers.GetLanguageContext(pythonEngine) as PythonContext;
|
|     try
|     {
|         scriptScope = pythonEngine.ImportModule(main);
|     }
|     catch (SystemExitException ex)
|     {
|         Console.WriteLine(ex.ToString());
|     }
| }

But the problem dated ahead about UTF-8 parsing again :(
To be fixed myself...



More information about the Ironpython-users mailing list