diff @pyobj/pyobj.m @ 202:3fa99babc7b5

pyeval: preliminary support for returning pyobj Introduces static method `pyobj.fromPythonVarName(s)`, whereas the constructor itself now no longer deals with `__InOct__` itself. * pyeval.cc: return the id() instead for now * @pyobj/pyobj.m: change constructor, add method * @pyobj/dummy.m: add doctests
author Colin Macdonald <cbm@m.fsf.org>
date Fri, 20 May 2016 00:20:03 -0700
parents 65fead19215f
children 7d03df51d6e8
line wrap: on
line diff
--- a/@pyobj/pyobj.m	Thu May 19 17:08:03 2016 -0700
+++ b/@pyobj/pyobj.m	Fri May 20 00:20:03 2016 -0700
@@ -53,9 +53,9 @@
   properties
     id
   end
-  methods
 
-    function x = pyobj(pyvarname)
+  methods(Static)
+    function x = fromPythonVarName(pyvarname)
       % if @var{pyvarname} is a string, its assumed to be a variable
       % name, e.g., previously created with pyexec.  This must exist
       % at the time of construction but it can disappear later (we
@@ -63,16 +63,23 @@
       if (~ ischar(pyvarname))
         error('pyobj: currently we only take variable names as input')
       end
-      % FIXME: check __InOct__ exists
-      % FIXME: ensure id is not in the dict
       cmd = sprintf ([ ...
         'if not ("__InOct__" in vars() or "__InOct__" in globals()):\n' ...
         '  __InOct__ = dict()\n' ...
         '__InOct__[hex(id(%s))] = %s' ], ...
         pyvarname, pyvarname);
       pyexec (cmd);
-      x.id = pyeval (['hex(id(' pyvarname '))']);
-      %x.repr = pyeval (['repr(' x.varname ')']);
+      id = pyeval (['hex(id(' pyvarname '))']);
+      x = pyobj(id);
+    end
+  end
+
+
+  methods
+    function x = pyobj(id)
+      % warning: not intended for casual use: you must also insert
+      % the object into the Python `__InOct__` dict with key `id`.
+      x.id = id;
     end
 
     function delete(x)
@@ -100,6 +107,8 @@
       delete(x)
     end
 
+    dummy (x)
+
     function r = getid (x)
       r = x.id;
     end