[IronPython] Loading DLL from pyc.py into ScriptEngine

Michael Foord fuzzyman at voidspace.org.uk
Wed Aug 27 00:12:09 CEST 2008


hellosticky wrote:
> I also tried:
>
>             Assembly assembly = Assembly.Load(AssemblyName);
>             ScriptRuntime runtime = ScriptRuntime.Create();
>             runtime.LoadAssembly(assembly);
>             ScriptScope scope = runtime.ExecuteFile("test.py");
>             object o = scope.Execute(null);
>
> But that threw a script not found exception saying it could find the source file for test.py, so it would seem like this wants to recompile it
>
>   
>> -----Original Message-----
>> From: hellosticky [mailto:hellosticky at gmail.com] 
>> Sent: Tuesday, August 26, 2008 5:52 PM
>> To: IronPython
>> Subject: Loading DLL from pyc.py into ScriptEngine
>>
>> Hi,
>>
>> I created an IronPython DLL with "ipy.exe pyc.py /out:test 
>> /target:dll test.py" which created test.dll. Now, from C#, 
>> I'd like to execute test.py from test.dll. I just opened up 
>> dlr-spec-hosting and there's a CreateScriptSourceFromStream, 
>> but I don't see that in 2.0 Beta 4 bits. Here is where I'm 
>> stuck. How do I get that assembly into the engine and then 
>> execute test.py?

Well - the easiest way is to execute 'import test' after having added 
the assembly. This should execute the code in order to create a module.

ScriptScope scope = engine.CreateScope();
ScriptSource source = engine.CreateScriptSourceFromString("import test", 
SourceCodeKind.Statements);
engine.Execute(scope);

There is an API for setting a stream as stdout on an engine - 
'runtime.IO.SetOutput'.

Michael

>>  Also, I'd like to get the output into a 
>> string (I guess I can just redirect Console.Out?)
>>
>> public string Execute(string assemblyName)
>> {
>>     string content = null;
>>
>>     Assembly assembly = Assembly.Load(assemblyName);
>>     ScriptRuntime runtime = ScriptRuntime.Create();
>>     ObjectOperations operations = runtime.CreateOperations();
>>     ScriptEngine engine = runtime.GetEngine("py");
>>     // magic..
>>
>>     return content;
>> }
>>
>> Thanks,
>>     
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.theotherdelia.co.uk/
http://www.resolverhacks.net/




More information about the Ironpython-users mailing list