[IronPython] Access to current Python engine in C# (Silverlight)

Michael Foord fuzzyman at voidspace.org.uk
Sat May 9 17:07:26 CEST 2009


William Reade wrote:
> I had a similar problem in Ironclad -- needing to get the engine which 
> called a C# method. Try adding a CodeContext parameter to GetModule 
> (which gets automagically inserted; no need to change the call from 
> Python), and then using PythonContext.GetContext to get a 
> PythonContext from the CodeContext. See the constructors in 
> http://code.google.com/p/ironclad/source/browse/trunk/src/Python25Mapper.cs 
>
>
> ...if your goggles can stand it.
>

I can see it - but I can't see how it helps. How do I go from there (and 
the LanguageContext I can extract from it) to the current engine?

I'm *fairly* sure I have the current engine anyway and that it is the 
importing scenario that is broken.

All the best,

Michael

> Michael Foord wrote:
>> Hello guys,
>>
>> I have a second use case for embedding IronPython in Silverlight. 
>> This is actually a dynamic application with a C# component that needs 
>> to programattically build a Python module.
>>
>> Again I have the same problem - imports in Python code fail. I would 
>> have expected that accessing the current runtime and fetching a 
>> Python engine would fetch the current Python engine, with the browser 
>> host correctly setup. Unfortunately that seems not to be the case. 
>> Can anyone spot problems with the following code:
>>
>>
>> using Microsoft.Scripting.Silverlight;
>> using IronPython;
>> using IronPython.Hosting;
>> using Microsoft.Scripting;
>> using Microsoft.Scripting.Hosting;
>>
>> namespace EmbeddedSLModule
>> {
>>    public class EmbeddedSLModule
>>    {
>>        private static string source = @"
>> import something
>> ";
>>        public static ScriptScope GetModule(){
>>            ScriptRuntime runtime = DynamicApplication.Current.Runtime;
>>            ScriptEngine engine = runtime.GetEngine("Python");
>>            ScriptScope scope = engine.CreateScope();
>>            ScriptSource script = 
>> engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
>>            script.Execute(scope);
>>
>>            return scope;
>>
>>        }
>>    }
>> }
>>
>>
>> It works fine for code that doesn't import anything - but imports 
>> from within the xap file fail.
>>
>> Thanks
>>
>> Michael Foord
>>
>
> _______________________________________________
> 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/blog





More information about the Ironpython-users mailing list