Mercurial > pytave
comparison 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 |
comparison
equal
deleted
inserted
replaced
251:c8da556b6793 | 278:9278a272b1c8 |
---|---|
70 | 70 |
71 Py_Initialize (); | 71 Py_Initialize (); |
72 | 72 |
73 object main_module = import ("__main__"); | 73 object main_module = import ("__main__"); |
74 object main_namespace = main_module.attr ("__dict__"); | 74 object main_namespace = main_module.attr ("__dict__"); |
75 #if PY_VERSION_HEX >= 0x03000000 | |
76 object builtins_module = import ("builtins"); | |
77 #else | |
78 object builtins_module = import ("__builtin__"); | |
79 #endif | |
80 | 75 |
81 try | 76 try |
82 { | 77 { |
83 res = eval (code.c_str (), main_namespace, main_namespace); | 78 res = eval (code.c_str (), main_namespace, main_namespace); |
84 // hex(id(res)) | |
85 object hex_function = builtins_module.attr ("hex"); | |
86 object id_function = builtins_module.attr ("id"); | |
87 object idtmp = hex_function (id_function (res)); | |
88 id = extract<std::string> (idtmp); | |
89 | 79 |
90 if (nargout > 0 || ! res.is_none ()) | 80 if (nargout > 0 || ! res.is_none ()) |
91 { | 81 { |
92 octave_value val; | 82 octave_value val; |
93 pytave::pyobj_to_octvalue (val, res); | 83 pytave::pyobj_to_octvalue (val, res); |
94 retval(0) = val; | 84 retval(0) = val; |
95 } | 85 } |
96 } | 86 } |
97 catch (pytave::object_convert_exception const &) | 87 catch (pytave::object_convert_exception const &) |
98 { | 88 { |
99 // Ensure we have a __InOct__ dict, and then put `res` into it | 89 error ("pyexec: error in return value type conversion"); |
100 exec ("if not (\"__InOct__\" in vars() or \"__InOct__\" in globals()):\n" | |
101 " __InOct__ = dict()\n" | |
102 " # FIXME: make it accessible elsewhere?\n" | |
103 " import __main__\n" | |
104 " __main__.__InOct__ = __InOct__\n", | |
105 main_namespace, main_namespace); | |
106 main_namespace["__InOct__"][id] = res; | |
107 // Create @pyobject | |
108 retval = feval ("pyobject", ovl (id), 1); | |
109 } | 90 } |
110 catch (error_already_set const &) | 91 catch (error_already_set const &) |
111 { | 92 { |
112 std::string message = pytave::fetch_exception_message (); | 93 std::string message = pytave::fetch_exception_message (); |
113 error ("pyeval: %s", message.c_str ()); | 94 error ("pyeval: %s", message.c_str ()); |
139 %!assert (class (pyeval ("True")), "logical") | 120 %!assert (class (pyeval ("True")), "logical") |
140 %!assert (class (pyeval ("False")), "logical") | 121 %!assert (class (pyeval ("False")), "logical") |
141 | 122 |
142 %!assert (isa (pyeval ("object()"), "pyobject")) | 123 %!assert (isa (pyeval ("object()"), "pyobject")) |
143 | 124 |
144 ## FIXME: these will change when dict, list, and tuple are not converted | 125 %!test |
145 %!assert (pyeval ("{'x': 1, 'y': 2}"), struct ("x", 1, "y", 2)) | 126 %! z = pyeval ("{'x': 1, 'y': 2}"); |
146 %!assert (pyeval ("[1, 2, 3]"), {1, 2, 3}) | 127 %! assert (isa (z, "pyobject")) |
147 %!assert (pyeval ("(4, 5, 6)"), {4, 5, 6}) | 128 %! assert (z{"x"}, 1) |
148 | 129 |
149 %!test | 130 %!test |
150 %! % FIXME: this will change when we stop converting lists | 131 %! z = pyeval ("[1, 2, 3]"); |
132 %! assert (isa (z, "pyobject")) | |
133 %! assert ({z{1}, z{2}, z{3}}, {1, 2, 3}) | |
134 | |
135 %!test | |
136 %! z = pyeval ("(4, 5, 6)"); | |
137 %! assert (isa (z, "pyobject")) | |
138 %! assert ({z{1}, z{2}, z{3}}, {4, 5, 6}) | |
139 | |
140 %!test | |
151 %! z = pyeval ("[1, [21, 22], 3, [41, [421, 422], 43]]"); | 141 %! z = pyeval ("[1, [21, 22], 3, [41, [421, 422], 43]]"); |
142 %! assert (isa (z, "pyobject")) | |
143 %! assert (isa (z{2}, "pyobject")) | |
152 %! assert (z{2}{1}, 21) | 144 %! assert (z{2}{1}, 21) |
153 %! assert (z{2}{2}, 22) | 145 %! assert (z{2}{2}, 22) |
146 %! assert (isa (z{4}{2}, "pyobject")) | |
154 %! assert (z{4}{2}{1}, 421) | 147 %! assert (z{4}{2}{1}, 421) |
155 %! assert (z{4}{2}{2}, 422) | 148 %! assert (z{4}{2}{2}, 422) |
156 | 149 |
157 %!error <NameError> | 150 %!error <NameError> |
158 %! pyexec ("def raiseException ():\n raise NameError ('oops')") | 151 %! pyexec ("def raiseException ():\n raise NameError ('oops')") |