[IronPython] Returning an object from a script

Curt Hagenlocher curt at hagenlocher.org
Thu Jul 3 17:31:27 CEST 2008


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>
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
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080703/f13a3998/attachment.html>


More information about the Ironpython-users mailing list