# HG changeset patch # User Mike Miller # Date 1469830523 25200 # Node ID 6e83efbcf1bc952c86faf585ea192fbb6408dd8c # Parent 3bdd924a5fc71110cb9127f2b17d9f753d162c4e# Parent 9278a272b1c8478e82c2b13362d4bb8645a1bc6b Merged in genuinelucifer/pytave_main (pull request #25) Drop conversion of lists, dicts and tuples (fixes issues #27, #26) diff -r 3bdd924a5fc7 -r 6e83efbcf1bc pycall.cc --- a/pycall.cc Fri Jul 29 14:54:15 2016 -0700 +++ b/pycall.cc Fri Jul 29 15:15:23 2016 -0700 @@ -218,16 +218,7 @@ } catch (pytave::object_convert_exception const &) { - // 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", - main_namespace, main_namespace); - main_namespace["__InOct__"][id] = res; - // Create @pyobject - retval = feval ("pyobject", ovl (id), 1); + error ("pyexec: error in return value type conversion"); } catch (pytave::value_convert_exception const &) { diff -r 3bdd924a5fc7 -r 6e83efbcf1bc pyeval.cc --- a/pyeval.cc Fri Jul 29 14:54:15 2016 -0700 +++ b/pyeval.cc Fri Jul 29 15:15:23 2016 -0700 @@ -72,20 +72,10 @@ object main_module = import ("__main__"); object main_namespace = main_module.attr ("__dict__"); -#if PY_VERSION_HEX >= 0x03000000 - object builtins_module = import ("builtins"); -#else - object builtins_module = import ("__builtin__"); -#endif try { res = eval (code.c_str (), main_namespace, main_namespace); - // hex(id(res)) - object hex_function = builtins_module.attr ("hex"); - object id_function = builtins_module.attr ("id"); - object idtmp = hex_function (id_function (res)); - id = extract (idtmp); if (nargout > 0 || ! res.is_none ()) { @@ -96,16 +86,7 @@ } catch (pytave::object_convert_exception const &) { - // 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", - main_namespace, main_namespace); - main_namespace["__InOct__"][id] = res; - // Create @pyobject - retval = feval ("pyobject", ovl (id), 1); + error ("pyexec: error in return value type conversion"); } catch (error_already_set const &) { @@ -145,16 +126,28 @@ %!assert (pyeval ("99999999999999"), 99999999999999) %!assert (pyeval ("-99999999999999"), -99999999999999) -## FIXME: these will change when dict, list, and tuple are not converted -%!assert (pyeval ("{'x': 1, 'y': 2}"), struct ("x", 1, "y", 2)) -%!assert (pyeval ("[1, 2, 3]"), {1, 2, 3}) -%!assert (pyeval ("(4, 5, 6)"), {4, 5, 6}) +%!test +%! z = pyeval ("{'x': 1, 'y': 2}"); +%! assert (isa (z, "pyobject")) +%! assert (z{"x"}, 1) + +%!test +%! z = pyeval ("[1, 2, 3]"); +%! assert (isa (z, "pyobject")) +%! assert ({z{1}, z{2}, z{3}}, {1, 2, 3}) %!test -%! % FIXME: this will change when we stop converting lists +%! z = pyeval ("(4, 5, 6)"); +%! assert (isa (z, "pyobject")) +%! assert ({z{1}, z{2}, z{3}}, {4, 5, 6}) + +%!test %! z = pyeval ("[1, [21, 22], 3, [41, [421, 422], 43]]"); +%! assert (isa (z, "pyobject")) +%! assert (isa (z{2}, "pyobject")) %! assert (z{2}{1}, 21) %! assert (z{2}{2}, 22) +%! assert (isa (z{4}{2}, "pyobject")) %! assert (z{4}{2}{1}, 421) %! assert (z{4}{2}{2}, 422) diff -r 3bdd924a5fc7 -r 6e83efbcf1bc python_to_octave.cc --- a/python_to_octave.cc Fri Jul 29 14:54:15 2016 -0700 +++ b/python_to_octave.cc Fri Jul 29 15:15:23 2016 -0700 @@ -36,6 +36,7 @@ #include #include #include +#include #include "arrayobjectdefs.h" #include "exceptions.h" @@ -474,6 +475,34 @@ return str; } + static void + pyobj_to_oct_pyobject (octave_value& oct_value, + const boost::python::object& py_object) + { + object main_module = import ("__main__"); + object main_namespace = main_module.attr ("__dict__"); +#if PY_VERSION_HEX >= 0x03000000 + object builtins_module = import ("builtins"); +#else + object builtins_module = import ("__builtin__"); +#endif + object hex_function = builtins_module.attr ("hex"); + object id_function = builtins_module.attr ("id"); + object idtmp = hex_function (id_function (py_object)); + std::string id = extract (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", + main_namespace, main_namespace); + main_namespace["__InOct__"][id] = py_object; + // Create @pyobject + oct_value = feval ("pyobject", ovl (id), 1); + } + void pyobj_to_octvalue (octave_value& oct_value, const boost::python::object& py_object) { @@ -502,18 +531,8 @@ oct_value = stringx (); else if (wstringx.check ()) oct_value = pyunicode_to_utf8 (py_object.ptr ()); - else if (listx.check ()) - pylist_to_cellarray (oct_value, (boost::python::list&)py_object); - else if (dictx.check ()) - pydict_to_octmap (oct_value, (boost::python::dict&)py_object); - else if (tuplex.check ()) - pytuple_to_cellarray (oct_value, (boost::python::tuple&)py_object); else - throw object_convert_exception ( - PyEval_GetFuncName (py_object.ptr ()) - + (PyEval_GetFuncDesc (py_object.ptr ()) - + std::string (": Unsupported Python object type, " - "cannot convert to Octave value"))); + pyobj_to_oct_pyobject (oct_value, py_object); } void pytuple_to_octlist (octave_value_list& octave_list,