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

Jimmy Schementi Jimmy.Schementi at microsoft.com
Thu Jun 4 23:14:46 CEST 2009


> Jimmy - did you get a chance to look at this?

Yes, sorry for the wait. Given something.py:

class Foo(object):
  attribute = "weeee"

The following will import it using the hosting API (written in Python because I don't have the SL tools installed at the moment):

code = """
import something
"""
runtime = DynamicApplication.Current.Runtime;

engine = runtime.GetEngine("Python")
scope = engine.CreateScope()
script = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements)
script.Execute(scope)
scope.something.Foo.attribute
# => weeee

Here's a repro of the behavior (run with script/server)
http://jimmy.schementi.com/silverlight/sl-py-import.zip

> If the code shown below *genuinely* gets a reference to the current engine then shouldn't the search path be setup already?

No, unfortunately the search path is set by DynamicApplication.InitializeDLR, which is only called when DynamicApplication is doing the code execution. I can make the search path be set in DynamicApplication.CreateRuntimeSetup, and then this should work fine. So even if you get DynamicApplication.Current.Runtime, if DynamicApplication hasn't run DLR code yet (which would be the case in a C#/VB hosted environment) you'll need to do:

runtime.Setup.Options["SearchPaths"] = new string[] { String.Empty };

The above example doesn't need this since it's written in Python, so DynamicApplication has run code and the search path has already been set. I can verify this works from C# as well, but let me know if you still have problems.

Was there any other SL questions you had? I have a bunch of emails starred but I'm not sure if they were all about this.

~Jimmy

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
Sent: Sunday, May 17, 2009 8:27 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Access to current Python engine in C# (Silverlight)

Jimmy - did you get a chance to look at this?

If the code shown below *genuinely* gets a reference to the current engine then shouldn't the search path be setup already?

Can you see what is wrong with the code below?

Thanks

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
>


--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog


_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com




More information about the Ironpython-users mailing list