Mercurial > pytave
comparison @pyobject/pyobject.m @ 347:7f039ffe501b
Store the _InOctave dict only in main
* @pyobject/pyobject.m: Simplified _InOctave storage.
* octave_to_python.cc: Rename _InOctave dict.
* pytave_utils.cc: Rename _InOctave dict.
* python_to_octave.cc: Simplified _InOctave storage.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Wed, 10 Aug 2016 11:34:19 -0700 |
parents | b1eff49bd139 |
children | 140e37e8e95a |
comparison
equal
deleted
inserted
replaced
346:b1eff49bd139 | 347:7f039ffe501b |
---|---|
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 | |
49 cmd = [ "if not getattr(__import__('__main__'), '_InOctave', None):\n" ... | |
50 " __import__('__main__')._InOctave = dict()" ]; | |
51 pyexec (cmd); | |
52 | |
48 ## XXX: perhaps not the ideal implementation! | 53 ## XXX: perhaps not the ideal implementation! |
49 vars = pyeval ("__import__('__main__').__dict__"); | 54 vars = pyeval ("__import__('__main__').__dict__"); |
50 ## this is vars{"_temp"} = x | 55 ## this is vars{"_temp"} = x |
51 idx = struct ("type", "{}", "subs", {{"_temp"}}); | 56 idx = struct ("type", "{}", "subs", {{"_temp"}}); |
52 vars = subsasgn (vars, idx, x); | 57 vars = subsasgn (vars, idx, x); |
53 cmd = [ ... | 58 |
54 "if not ('__InOct__' in vars() or '__InOct__' in globals()):\n" ... | 59 pyexec ("__import__('__main__')._InOctave[hex(id(_temp))] = _temp") |
55 " __InOct__ = dict()\n" ... | |
56 " # FIXME: make it accessible elsewhere?\n" ... | |
57 " import __main__\n" ... | |
58 " __main__.__InOct__ = __InOct__\n" ... | |
59 "__InOct__[hex(id(_temp))] = _temp" ]; | |
60 pyexec (cmd); | |
61 id = pyeval (["hex(id(_temp))"]); | 60 id = pyeval (["hex(id(_temp))"]); |
62 obj = pyobject (0, id); | 61 obj = pyobject (0, id); |
63 endif | 62 endif |
64 return | 63 return |
65 endif | 64 endif |
66 | 65 |
67 if (nargin == 2) | 66 if (nargin == 2) |
68 ## The actual constructor. Nicer to split this off to static method | 67 ## The actual constructor. Nicer to split this off to static method |
69 ## 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. |
70 ## Warning: not intended for casual use: you must also insert the | 69 ## Warning: not intended for casual use: you must also insert the |
71 ## object into the Python `__InOct__` dict with key `id`. | 70 ## object into the Python `_InOctave` dict with key `id`. |
72 obj.id = id; | 71 obj.id = id; |
73 return | 72 return |
74 endif | 73 endif |
75 | 74 |
76 error ("pyobject: unexpected input to the constructor") | 75 error ("pyobject: unexpected input to the constructor") |
93 # @end example | 92 # @end example |
94 | 93 |
95 #disp ("delete") | 94 #disp ("delete") |
96 | 95 |
97 # throws KeyError if it wasn't in there for some reason | 96 # throws KeyError if it wasn't in there for some reason |
98 cmd = sprintf ("__InOct__.pop('%s')", x.id); | 97 cmd = sprintf ("__import__('__main__')._InOctave.pop('%s')", x.id); |
99 pyexec (cmd) | 98 pyexec (cmd) |
100 endfunction | 99 endfunction |
101 | 100 |
102 # methods defined in external files | 101 # methods defined in external files |
103 dummy (x) | 102 dummy (x) |