[IronPython] Returning an object from a script

Barry Carr barrycarr at ixian-software.com
Thu Jul 3 17:40:17 CEST 2008


Hi Curt,

Thanks very much. That worked a treat. I'm new to the DLR and IP2 so I didn't know about GetVariable.

Cheers
Barry Carr

Curt Hagenlocher wrote:
> You never actually assign a value to "obj".  SetVariable doesn't create 
> a linkage between your local variable and the script-defined one.  You 
> have to explicitly fetch out the value of "instance" by using something like
>  
> IMigration instance = scope.GetVariable<IMigration>("instance").
> 
> On Thu, Jul 3, 2008 at 7:34 AM, Barry Carr <barrycarr at ixian-software.com 
> <mailto:barrycarr at ixian-software.com>> wrote:
> 
>     Hi,
> 
>     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;
>     begin
>      PrepPython;
> 
>      var script := pe.CreateScriptSourceFromFile(
>     ".\\PythonHostingTest.py" );
>      var scope  := env.CreateScope;
>      var obj : Object := nil;
>      scope.SetVariable("instance", obj );
>      script.Execute(scope);
>      IMigration(obj).Up;                     // <-- Null ref exception here
>      Console.WriteLine('Hello World.');
>     end;
> 
>     class method ConsoleApp.PrepPython;
>     begin
>      env := ScriptRuntime.Create;
>      env.LoadAssembly( typeOf(String).Assembly );
>      env.LoadAssembly( typeOf(Debug).Assembly );
>      env.LoadAssembly( typeOf(IMigration).Assembly );
>      pe := env.GetEngine("py");
>     end;
> 
>     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.
> 
>     Cheers
>     Barry Carr
>     _______________________________________________
>     Users mailing list
>     Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>     http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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