changeset 217:69abda471c67

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__
author Colin Macdonald <cbm@m.fsf.org>
date Sat, 28 May 2016 10:18:39 -0700
parents 3b2b7db7d709
children 02da92723889
files @pyobject/dummy.m @pyobject/pyobject.m octave_to_python.cc pyeval.cc
diffstat 4 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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{} <module 'sys' (built-in)>
+%% @end group
+%% @end example
+%%
 %% @seealso{pyobject}
 %% @end defmethod
 
--- 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);
--- 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 <octave/oct.h>
 #include <octave/ov.h>
 #include <octave/oct-map.h>
+#include <octave/parse.h>
 
 #include <iostream>
 #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");
   }
--- 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