Clr.AddReference basically does two things. One it loads the dll and two it pokes into the dll to see if there are any compiled scriptcodes. If there are any, it then inserts a CompiledLoader into sys.meta_path and lets the CompiledLoader know about the these compiled scriptcodes - in this case one called test.py
After that when an import is done, the normal import mechanism kicks in which looks at meta_path and asks the CompiledLoader as well to find out if it knows about "test" and CompiledLoader returns the scriptcode. As with any import then the code is executed. Hope that makes sense. Michael, this will also answer your question as to why runtime.LoadAssembly will not work. The compiledLoader needs tobe informed about the script and that's a python specific thing. -----Original Message----- From: hellosticky [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 26, 2008 3:46 PM To: Srivatsn Narayanan; 'Discussion of IronPython' Subject: RE: [IronPython] Loading DLL from pyc.py into ScriptEngine Thanks, I will try that. Can you explain again how just doing "import test" executes the test.py file that was compiled into the DLL? It's not very logical.. Thanks, > -----Original Message----- > From: Srivatsn Narayanan [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 26, 2008 6:34 PM > To: Discussion of IronPython; [EMAIL PROTECTED] > Subject: RE: [IronPython] Loading DLL from pyc.py into ScriptEngine > > Also Assembly.Load is not sufficient to be able to import the > compiled code. A clr.AddReference needs to be done which > publishes the module. The easiest way to do that is include > it in the string as Michael pointed out. > > ScriptSource source = > engine.CreateScriptSourceFromString("import > clr;clr.AddReference(\"test.dll\");import test", > SourceCodeKind.Statements); > > You might need to add the current folder to the path as well > before the clr.AddReference. > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Michael Foord > Sent: Tuesday, August 26, 2008 3:12 PM > To: [EMAIL PROTECTED]; Discussion of IronPython > Subject: Re: [IronPython] Loading DLL from pyc.py into ScriptEngine > > 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:[EMAIL PROTECTED] > >> 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 > > [email protected] > > 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/ > > _______________________________________________ > Users mailing list > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > _______________________________________________ Users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
