[IronPython] Custom Host - Import CLR fails

Ben Hall ben2004uk at googlemail.com
Sat May 17 03:09:05 CEST 2008


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



More information about the Ironpython-users mailing list