[IronPython] Returning an object from a script

Barry Carr barrycarr at ixian-software.com
Thu Jul 3 16:34:12 CEST 2008


Using IP2B3 (on .NET 2,0), is it possible to get a IronPython script to create an object and return 
it or pass it back to the host application to be used later?

I tried what I thought was the obvious route but got a null reference exception when I tried to use 
the returned object (the code is Oxygene - an Object Pascal variant):

class method ConsoleApp.Main;

   var script := pe.CreateScriptSourceFromFile( ".\\PythonHostingTest.py" );
   var scope  := env.CreateScope;
   var obj : Object := nil;
   scope.SetVariable("instance", obj );
   IMigration(obj).Up;                     // <-- Null ref exception here
   Console.WriteLine('Hello World.');

class method ConsoleApp.PrepPython;
   env := ScriptRuntime.Create;
   env.LoadAssembly( typeOf(String).Assembly );
   env.LoadAssembly( typeOf(Debug).Assembly );
   env.LoadAssembly( typeOf(IMigration).Assembly );
   pe := env.GetEngine("py");

And here is my IronPython Script:

import System
from GeoMEM.NETandCF.Migrations import *

class Hello(IMigration):
     def __init__( self, msg = "A BIIIG Hello From Python" ):
         self.msg = msg

     def Up(self):
         print self.msg

     def Down(self):
         print "Down"

instance = Hello()

Am I close or barking up the wrong tree? Thanks.

Barry Carr

More information about the Ironpython-users mailing list