Mercurial > pytave
changeset 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 | d4744d829ee0 |
files | @pyobject/pyobject.m oct-py-util.cc octave_to_python.cc python_to_octave.cc |
diffstat | 4 files changed, 17 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/@pyobject/pyobject.m Tue Aug 16 16:08:43 2016 -0700 +++ b/@pyobject/pyobject.m Wed Aug 10 11:34:19 2016 -0700 @@ -45,19 +45,18 @@ if (isa (x, "pyobject")) obj = x; else + ## Ensure _InOctave dict exists + cmd = [ "if not getattr(__import__('__main__'), '_InOctave', None):\n" ... + " __import__('__main__')._InOctave = dict()" ]; + pyexec (cmd); + ## XXX: perhaps not the ideal implementation! vars = pyeval ("__import__('__main__').__dict__"); ## this is vars{"_temp"} = x idx = struct ("type", "{}", "subs", {{"_temp"}}); vars = subsasgn (vars, idx, x); - cmd = [ ... - "if not ('__InOct__' in vars() or '__InOct__' in globals()):\n" ... - " __InOct__ = dict()\n" ... - " # FIXME: make it accessible elsewhere?\n" ... - " import __main__\n" ... - " __main__.__InOct__ = __InOct__\n" ... - "__InOct__[hex(id(_temp))] = _temp" ]; - pyexec (cmd); + + pyexec ("__import__('__main__')._InOctave[hex(id(_temp))] = _temp") id = pyeval (["hex(id(_temp))"]); obj = pyobject (0, id); endif @@ -68,7 +67,7 @@ ## The actual constructor. Nicer to split this off to static method ## like `pyobject.new` but I don't know how to call from pycall.cc. ## Warning: not intended for casual use: you must also insert the - ## object into the Python `__InOct__` dict with key `id`. + ## object into the Python `_InOctave` dict with key `id`. obj.id = id; return endif @@ -95,7 +94,7 @@ #disp ("delete") # throws KeyError if it wasn't in there for some reason - cmd = sprintf ("__InOct__.pop('%s')", x.id); + cmd = sprintf ("__import__('__main__')._InOctave.pop('%s')", x.id); pyexec (cmd) endfunction
--- a/oct-py-util.cc Tue Aug 16 16:08:43 2016 -0700 +++ b/oct-py-util.cc Wed Aug 10 11:34:19 2016 -0700 @@ -92,7 +92,7 @@ { octave_value_list tmp = feval ("getid", ovl (oct_value), 1); std::string hexid = tmp(0).string_value (); - py_object = main_module.attr ("__InOct__")[hexid]; + py_object = main_module.attr ("_InOctave")[hexid]; } }
--- a/octave_to_python.cc Tue Aug 16 16:08:43 2016 -0700 +++ b/octave_to_python.cc Wed Aug 10 11:34:19 2016 -0700 @@ -190,8 +190,7 @@ { octave_value_list tmp = feval ("getid", ovl (octvalue), 1); std::string hexid = tmp(0).string_value (); - // FIXME: added a messy ref to __InOct__ in __main__, find a better way - py_object = boost::python::import ("__main__").attr ("__InOct__")[hexid]; + py_object = boost::python::import ("__main__").attr ("_InOctave")[hexid]; } else throw value_convert_exception (
--- a/python_to_octave.cc Tue Aug 16 16:08:43 2016 -0700 +++ b/python_to_octave.cc Wed Aug 10 11:34:19 2016 -0700 @@ -323,14 +323,13 @@ object idtmp = hex_function (id_function (py_object)); std::string id = extract<std::string> (idtmp); - // 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" - " # FIXME: make it accessible elsewhere?\n" - " import __main__\n" - " __main__.__InOct__ = __InOct__\n", + // Ensure _InOctave dict exists + // TODO: don't need exec, just do here in C + exec ("if not getattr(__import__('__main__'), '_InOctave', None):\n" + " __import__('__main__')._InOctave = dict()", main_namespace, main_namespace); - main_namespace["__InOct__"][id] = py_object; + + main_namespace["_InOctave"][id] = py_object; // Create @pyobject oct_value = feval ("pyobject", ovl (0, id), 2); }