[IronPython] Custom Host - Import CLR fails

Dino Viehland dinov at exchange.microsoft.com
Sat May 17 04:01:45 CEST 2008


This works for me in IronPython 2.0B2:

using System;
using System.Reflection;

using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

class Test {
    public static void Main(string[]args) {
            ScriptRuntime runtime = ScriptRuntime.Create();
            ScriptEngine engine = runtime.GetEngine("py");

            runtime.LoadAssembly(typeof(string).Assembly);
            runtime.LoadAssembly(typeof(System.Diagnostics.Debug).Assembly);

            Assembly assem = Assembly.GetAssembly(Type.GetType("System.Text.StringBuilder"));
            engine.Runtime.LoadAssembly(assem);

            //ScriptSource code = engine.CreateScriptSourceFromString("import clr\n", SourceCodeKind.File);
            ScriptScope source = engine.CreateScope();
            source.Execute("import clr\nprint clr\nimport System\nSystem.Console.WriteLine('hello world')\n");

    }
}

It prints:

<module 'clr' (built-in)>
hello world

Do you still get an exception with this code?  I also realize you were trying to load them dynamically on another thread but it might be worth pointing out this doesn't actually end up w/ any dependencies on IronPython.dll or IronPython.Modules.dll as written.


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ben Hall
Sent: Friday, May 16, 2008 6:09 PM
To: Discussion of IronPython
Subject: [IronPython] Custom Host - Import CLR fails

Hi,

After getting Python code to work successfully, I decided to quickly
import the CLR and access some .Net objects. At this point my code
blew up.
The code I tried to execute was "import clr".

Exception is this:

Unhandled Exception System.NullReferenceException: Object reference
not set to an instance of an object.
   at Microsoft.Scripting.Utils.SlowReflectedCaller.InvokeInstance(Object
instance, Object[] args)
   at Microsoft.Scripting.Actions.ActionBinder.UpdateSiteAndExecute[T](CodeContext
context, CallSite`1 site, Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update5[T0,T1,T2,T3,T4,TRet](CallSite
site, CodeContext context, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4
arg4)
   at Microsoft.Scripting.Actions.DynamicSite`6.Invoke(CodeContext
context, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
   at IronPython.Runtime.Importer.Import(CodeContext context, String
fullName, PythonTuple from, Int32 level)
   at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext
context, String fullName, Int32 level)
   at Initialize##1(Closure , CodeContext )
   at Microsoft.Scripting.ScriptCode.Run(CodeContext context, Boolean
tryEvaluate)
   at Microsoft.Scripting.ScriptCode.Run(Scope scope, Boolean tryEvaluate)
   at Microsoft.Scripting.ScriptCode.Run(Scope scope)
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
   at Microsoft.Scripting.Hosting.ScriptScope.Execute(String code)
   at MyApp.Engine.ScriptExecutor.ExecuteStatement(String code)

After a bit of search, I found that you don't load mscorlib for us.

I have tried 3 different ways to import the assemblies everyone says I
need to load:

            ScriptRuntime runtime = ScriptRuntime.Create();
            ScriptEngine engine = runtime.GetEngine();

            LanguageContext Language =
HostingHelpers.GetLanguageContext(engine);
            Language.DomainManager.LoadAssembly(typeof(string).Assembly);
            Language.DomainManager.LoadAssembly(typeof(System.Diagnostics.Debug).Assembly);

            runtime.LoadAssembly(typeof(string).Assembly);
            runtime.LoadAssembly(typeof(System.Diagnostics.Debug).Assembly);

            Assembly assem =
Assembly.GetAssembly(Type.GetType("System.Text.StringBuilder"));
            engine.Runtime.LoadAssembly(assem);

To execute the code, I do this:
            ScriptScope source = Engine.CreateScope();
            source.Execute(code);

However nothing, I still get the exception.

Any suggestions on where I am going wrong?

Thanks

Ben Hall
Blog.BenHall.me.uk
_______________________________________________
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