Mercurial > pytave
diff pyeval.cc @ 278:9278a272b1c8
Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
* python_to_octave.cc: Remove the logic that attempts to convert lists, dicts and
tuples from python to octave cell arrays. Instead convert them to pyobjects.
* pycall.cc, pyeval.cc: Do not attempt conversion to pyobject and rather issue
proper error message for the exception. Change tests to consider this change.
author | Abhinav Tripathi <genuinelucifer@gmail.com> |
---|---|
date | Thu, 28 Jul 2016 13:50:54 -0700 |
parents | c3addc52dfcd |
children | 6e83efbcf1bc |
line wrap: on
line diff
--- a/pyeval.cc Wed Jul 27 20:30:35 2016 -0700 +++ b/pyeval.cc Thu Jul 28 13:50:54 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<std::string> (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 &) { @@ -141,16 +122,28 @@ %!assert (isa (pyeval ("object()"), "pyobject")) -## 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)