Mercurial > pytave
comparison @pyobject/pyobject.m @ 350:e89a8a37fd8a
Rename the Octave-Python communication dict
* @pyobject/pyobject.m: Rename dict.
* octave_to_python.cc: Rename dict.
* pytave_utils.cc: Rename dict.
* python_to_octave.cc: Rename dict.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Thu, 11 Aug 2016 10:29:18 -0700 |
parents | 140e37e8e95a |
children | 6cd581661176 |
comparison
equal
deleted
inserted
replaced
349:140e37e8e95a | 350:e89a8a37fd8a |
---|---|
43 if (nargin == 1) | 43 if (nargin == 1) |
44 ## Convert the input to a pyobject | 44 ## Convert the input to a pyobject |
45 if (isa (x, "pyobject")) | 45 if (isa (x, "pyobject")) |
46 obj = x; | 46 obj = x; |
47 else | 47 else |
48 ## Ensure _InOctave dict exists | 48 ## Ensure dict for Octave communication exists |
49 cmd = [ "if not getattr(__import__('__main__'), '_InOctave', None):\n" ... | 49 cmd = [ "if not getattr(__import__('__main__'), '_in_octave', None):\n" ... |
50 " __import__('__main__')._InOctave = dict()" ]; | 50 " __import__('__main__')._in_octave = dict()" ]; |
51 pyexec (cmd); | 51 pyexec (cmd); |
52 | 52 |
53 ## Function to insert and return the hex id | 53 ## Function to insert and return the hex id |
54 cmd = [ "def _in_octave_insert(x):\n" ... | 54 cmd = [ "def _in_octave_insert(x):\n" ... |
55 " h = hex(id(x))\n" ... | 55 " h = hex(id(x))\n" ... |
56 " __import__('__main__')._InOctave[h] = x\n" ... | 56 " __import__('__main__')._in_octave[h] = x\n" ... |
57 " return h" ]; | 57 " return h" ]; |
58 pyexec (cmd); | 58 pyexec (cmd); |
59 | 59 |
60 id = pycall ("_in_octave_insert", x); | 60 id = pycall ("_in_octave_insert", x); |
61 obj = pyobject (0, id); | 61 obj = pyobject (0, id); |
65 | 65 |
66 if (nargin == 2) | 66 if (nargin == 2) |
67 ## The actual constructor. Nicer to split this off to static method | 67 ## The actual constructor. Nicer to split this off to static method |
68 ## like `pyobject.new` but I don't know how to call from pycall.cc. | 68 ## like `pyobject.new` but I don't know how to call from pycall.cc. |
69 ## Warning: not intended for casual use: you must also insert the | 69 ## Warning: not intended for casual use: you must also insert the |
70 ## object into the Python `_InOctave` dict with key `id`. | 70 ## object into the Python `_in_octave` dict with key `id`. |
71 obj.id = id; | 71 obj.id = id; |
72 return | 72 return |
73 endif | 73 endif |
74 | 74 |
75 error ("pyobject: unexpected input to the constructor") | 75 error ("pyobject: unexpected input to the constructor") |
91 # @print{} ... KeyError ... | 91 # @print{} ... KeyError ... |
92 # @end example | 92 # @end example |
93 | 93 |
94 #disp ("delete") | 94 #disp ("delete") |
95 | 95 |
96 # throws KeyError if it wasn't in there for some reason | 96 ## throws KeyError if it wasn't in there for some reason |
97 cmd = sprintf ("__import__('__main__')._InOctave.pop('%s')", x.id); | 97 cmd = sprintf ("__import__('__main__')._in_octave.pop('%s')", x.id); |
98 pyexec (cmd) | 98 pyexec (cmd) |
99 endfunction | 99 endfunction |
100 | 100 |
101 # methods defined in external files | 101 # methods defined in external files |
102 dummy (x) | 102 dummy (x) |