# HG changeset patch # User Colin Macdonald # Date 1464455919 25200 # Node ID 69abda471c67797d31ecfc4b6e7d9e5499a782b7 # Parent 3b2b7db7d709878cb1c906cc08996539d33389b8 Attempt at moving pyobject from octave to python * @pyobject/pyobject.m: keep ref to __InOct__ in __main__ * pyeval: keep ref to __InOct__ in __main__ * @pyobject/dummy.m: add pycall doctest * octave_to_python.cc: if we see pyobject, get original from __InOct__ diff -r 3b2b7db7d709 -r 69abda471c67 @pyobject/dummy.m --- a/@pyobject/dummy.m Fri May 27 14:13:34 2016 -0700 +++ b/@pyobject/dummy.m Sat May 28 10:18:39 2016 -0700 @@ -113,6 +113,16 @@ %% @end group %% @end example %% +%% A @@pyobject can be passed back to Python. This does not make +%% a copy but rather a reference to the original object. +%% For example: +%% @example +%% @group +%% pycall('__builtin__.print', sysmodule) +%% @print{} +%% @end group +%% @end example +%% %% @seealso{pyobject} %% @end defmethod diff -r 3b2b7db7d709 -r 69abda471c67 @pyobject/pyobject.m --- a/@pyobject/pyobject.m Fri May 27 14:13:34 2016 -0700 +++ b/@pyobject/pyobject.m Sat May 28 10:18:39 2016 -0700 @@ -42,7 +42,10 @@ end cmd = sprintf ([ ... 'if not ("__InOct__" in vars() or "__InOct__" in globals()):\n' ... - ' __InOct__ = dict()\n' ... + ' __InOct__ = dict()\n' ... + ' # FIXME: make it accessible elsewhere?\n' ... + ' import __main__\n' ... + ' __main__.__InOct__ = __InOct__\n' ... '__InOct__[hex(id(%s))] = %s' ], ... pyvarname, pyvarname); pyexec (cmd); diff -r 3b2b7db7d709 -r 69abda471c67 octave_to_python.cc --- a/octave_to_python.cc Fri May 27 14:13:34 2016 -0700 +++ b/octave_to_python.cc Sat May 28 10:18:39 2016 -0700 @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "arrayobjectdefs.h" @@ -221,7 +222,13 @@ octvalue_to_pyarr (py_object, octvalue); else if (octvalue.is_map ()) octmap_to_pyobject (py_object, octvalue.map_value ()); - else + else if (octvalue.is_object ()) { + octave_value_list tmp = feval ("getid", ovl (octvalue), 1); + std::string hexid = tmp(0).string_value(); + //std::cerr << "passed in hexid: " << hexid << std::endl; + // FIXME: added a messy ref to __InOct__ in __main__, find a better way + py_object = boost::python::import("__main__").attr("__InOct__")[hexid]; + } else throw value_convert_exception ( "Conversion from Octave value not implemented"); } diff -r 3b2b7db7d709 -r 69abda471c67 pyeval.cc --- a/pyeval.cc Fri May 27 14:13:34 2016 -0700 +++ b/pyeval.cc Sat May 28 10:18:39 2016 -0700 @@ -92,7 +92,10 @@ { // Ensure we have a __InOct__ dict, and then put `res` into it exec ("if not (\"__InOct__\" in vars() or \"__InOct__\" in globals()):\n" - " __InOct__ = dict()\n", + " __InOct__ = dict()\n" + " # FIXME: make it accessible elsewhere?\n" + " import __main__\n" + " __main__.__InOct__ = __InOct__\n", main_namespace, main_namespace); main_namespace["__InOct__"][id] = res; // Create @pyobject