Re: [IronPython] Silverlight Application Hosting IronPython
Jimmy Schementi wrote: On Sat, May 9, 2009 at 4:30 AM, Michael Foord fuzzy...@voidspace.org.uk mailto:fuzzy...@voidspace.org.uk wrote: Hello guys, I'm having problems with a C# Silverlight application that hosts IronPython. First of all, this page http://sdlsdk.codeplex.com/Wiki/View.aspx?title=Hosting implies that the following code should load assemblies for the hosted IronPython code: foreach (string name in new string[] { mscorlib, System, System.Windows, System.Windows.Browser, System.Net }) { runtime.Host.PlatformAdaptationLayer.LoadAssembly(name); } It doesn't - an import from System.Windows fails. The following code works as in a normal hosted environment: runtime.LoadAssembly(typeof(String).Assembly); runtime.LoadAssembly(typeof(Uri).Assembly); runtime.LoadAssembly(typeof(Canvas).Assembly); The documentation is wrong; always trust what the code in Microsoft.Scripting.Silverlight.DynamicApplication does the equivalent of: foreach (string name in new string[] { mscorlib, System, System.Windows, System.Windows.Browser, System.Net }) { runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly(name)); } The PAL.LoadAssembly is able to find assemblies in the XAP or platform and loads them, but you always need to call runtime.LoadAssembly to have it loaded on the runtime. Why not just do this: runtime.LoadAssembly(typeof(String).Assembly); runtime.LoadAssembly(typeof(Uri).Assembly); runtime.LoadAssembly(typeof(Canvas).Assembly); It works fine, so why use the PlatformAdaptationLayer? FYI, Hosted IronPython code is pretty much the same as non-hosted ... just that the non-hosted scenario does the hosting for you (with the DynamicApplication type). So as long as you're mimicking how DynamicApplication does things, you should be fine. I did try reading DynamicApplication, but it is much more general and so it is hard to follow the specific code path for the case when you are creating an IronPython application. I should probably have tried for longer before giving up... Secondly, which is much worse but may be related, imports from Python files inside the xap fail when IronPython is hosted. I am using the following code: ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null); setup.HostType = typeof(Microsoft.Scripting.Silverlight.BrowserScriptHost); ScriptRuntime runtime = new ScriptRuntime(setup); ScriptEngine pe = Python.GetEngine(runtime); ScriptScope scope = pe.CreateScope(); ScriptSource source = pe.CreateScriptSourceFromString(python_source, SourceCodeKind.Statements); source.Execute(scope); How do I setup the platform adaptation layer so that it can import from inside the xap? Again, doing exactly what DynamicApplication does is key =) I believe you need to set the SearchPaths option so it knows where to look for source files. In Silverlight, is the root of the XAP file, so this should do the trick: setup.Options[SearchPaths] = new string[] { String.Empty }; This works. Thank you very much. :-) All the best, Michael Foord All the best, Michael Let me know if this doesn't work for you, and I can send you an app that does. =) ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com -- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Silverlight Application Hosting IronPython
On Sat, May 9, 2009 at 4:30 AM, Michael Foord fuzzy...@voidspace.org.uk mailto:fuzzy...@voidspace.org.uk wrote: Hello guys, I'm having problems with a C# Silverlight application that hosts IronPython. First of all, this page http://sdlsdk.codeplex.com/Wiki/View.aspx?title=Hosting implies that the following code should load assemblies for the hosted IronPython code: foreach (string name in new string[] { mscorlib, System, System.Windows, System.Windows.Browser, System.Net }) { runtime.Host.PlatformAdaptationLayer.LoadAssembly(name); } It doesn't - an import from System.Windows fails. The following code works as in a normal hosted environment: runtime.LoadAssembly(typeof(String).Assembly); runtime.LoadAssembly(typeof(Uri).Assembly); runtime.LoadAssembly(typeof(Canvas).Assembly); The documentation is wrong; always trust what the code in Microsoft.Scripting.Silverlight.DynamicApplication does the equivalent of: foreach (string name in new string[] { mscorlib, System, System.Windows, System.Windows.Browser, System.Net }) { runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly(name)); } The PAL.LoadAssembly is able to find assemblies in the XAP or platform and loads them, but you always need to call runtime.LoadAssembly to have it loaded on the runtime. FYI, Hosted IronPython code is pretty much the same as non-hosted ... just that the non-hosted scenario does the hosting for you (with the DynamicApplication type). So as long as you're mimicking how DynamicApplication does things, you should be fine. Secondly, which is much worse but may be related, imports from Python files inside the xap fail when IronPython is hosted. I am using the following code: ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null); setup.HostType = typeof(Microsoft.Scripting.Silverlight.BrowserScriptHost); ScriptRuntime runtime = new ScriptRuntime(setup); ScriptEngine pe = Python.GetEngine(runtime); ScriptScope scope = pe.CreateScope(); ScriptSource source = pe.CreateScriptSourceFromString(python_source, SourceCodeKind.Statements); source.Execute(scope); How do I setup the platform adaptation layer so that it can import from inside the xap? Again, doing exactly what DynamicApplication does is key =) I believe you need to set the SearchPaths option so it knows where to look for source files. In Silverlight, is the root of the XAP file, so this should do the trick: setup.Options[SearchPaths] = new string[] { String.Empty }; All the best, Michael Let me know if this doesn't work for you, and I can send you an app that does. =) ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Silverlight Application Hosting IronPython
I don't know hardly anything about Silverlight, but it might be useful to know what error message you're getting when you fail to load System.Windows. Or if I misread and you're actually failing to import something from the System.Windows namespace in Python code, then it would be good to know what symbols you're trying to import. Hopefully Jimmy will have something to say about the XAP import issue now that he's back from RailsConf :). On Sat, May 9, 2009 at 4:30 AM, Michael Foord fuzzy...@voidspace.org.ukwrote: Hello guys, I'm having problems with a C# Silverlight application that hosts IronPython. First of all, this page http://sdlsdk.codeplex.com/Wiki/View.aspx?title=Hosting implies that the following code should load assemblies for the hosted IronPython code: foreach (string name in new string[] { mscorlib, System, System.Windows, System.Windows.Browser, System.Net }) { runtime.Host.PlatformAdaptationLayer.LoadAssembly(name); } It doesn't - an import from System.Windows fails. The following code works as in a normal hosted environment: runtime.LoadAssembly(typeof(String).Assembly); runtime.LoadAssembly(typeof(Uri).Assembly); runtime.LoadAssembly(typeof(Canvas).Assembly); Secondly, which is much worse but may be related, imports from Python files inside the xap fail when IronPython is hosted. I am using the following code: ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null); setup.HostType = typeof(Microsoft.Scripting.Silverlight.BrowserScriptHost); ScriptRuntime runtime = new ScriptRuntime(setup); ScriptEngine pe = Python.GetEngine(runtime); ScriptScope scope = pe.CreateScope(); ScriptSource source = pe.CreateScriptSourceFromString(python_source, SourceCodeKind.Statements); source.Execute(scope); How do I setup the platform adaptation layer so that it can import from inside the xap? All the best, Michael -- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com