Mercurial > pytave
comparison python_to_octave.cc @ 362:b0677c492655
Overhaul Python object storage and wrapping in pyobject
* oct-py-util.cc, oct-py-util.h (pytave::py_objstore_del,
pytave::py_objstore_get, pytave::py_objstore_put, pytave::pyobject_wrap_object,
pytave::pyobject_unwrap_object): New functions.
(pytave::get_object_from_python): Use pyobject_unwrap_object.
* octave_to_python.cc (pytave::octvalue_to_pyobj): Use pyobject_unwrap_object.
* python_to_octave.cc (pytave::pyobj_to_octvalue): Use pyobject_wrap_object.
(pytave::pyobj_to_oct_pyobject): Delete.
* __py_struct_from_dict__.cc (F__py_objstore_del__, F__py_objstore_get__,
F__py_objstore_put__): New functions.
* @pyobject/pyobject.m (pyobject.m_id): Rename from id.
(pyobject.pyobject): Use __py_objstore_put__, simplify conditional logic.
(pyobject.delete): Use __py_objstore_del__. (pyobject.id): Rename from getid.
* @pyobject/display.m: Use pyobject.id method name, format as hex.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 25 Aug 2016 09:51:58 -0700 |
parents | 826a23f63f75 |
children | 087e7bc3697f |
comparison
equal
deleted
inserted
replaced
361:07c1b457cb6b | 362:b0677c492655 |
---|---|
308 + ": Encountered unsupported Python array"); | 308 + ": Encountered unsupported Python array"); |
309 break; | 309 break; |
310 } | 310 } |
311 } | 311 } |
312 | 312 |
313 static void | |
314 pyobj_to_oct_pyobject (octave_value& oct_value, | |
315 const boost::python::object& py_object) | |
316 { | |
317 object main_module = import ("__main__"); | |
318 object builtins_module; | |
319 pytave::get_builtins_module (builtins_module); | |
320 object hex_function = builtins_module.attr ("hex"); | |
321 object id_function = builtins_module.attr ("id"); | |
322 object idtmp = hex_function (id_function (py_object)); | |
323 std::string id = extract<std::string> (idtmp); | |
324 | |
325 // Ensure dict for Octave communication exists | |
326 if (! PyObject_HasAttrString (main_module.ptr (), "_in_octave")) | |
327 main_module.attr ("_in_octave") = boost::python::dict (); | |
328 | |
329 main_module.attr ("_in_octave")[id] = py_object; | |
330 // Create @pyobject | |
331 oct_value = feval ("pyobject", ovl (0, id), 1)(0); | |
332 } | |
333 | |
334 void pyobj_to_octvalue (octave_value& oct_value, | 313 void pyobj_to_octvalue (octave_value& oct_value, |
335 const boost::python::object& py_object) | 314 const boost::python::object& py_object) |
336 { | 315 { |
337 extract<numeric::array> arrayx (py_object); | 316 extract<numeric::array> arrayx (py_object); |
338 | 317 |
349 else if (arrayx.check ()) | 328 else if (arrayx.check ()) |
350 pyarr_to_octvalue (oct_value, (PyArrayObject*)py_object.ptr ()); | 329 pyarr_to_octvalue (oct_value, (PyArrayObject*)py_object.ptr ()); |
351 else if (PyBytes_Check (py_object.ptr ()) || PyUnicode_Check (py_object.ptr ())) | 330 else if (PyBytes_Check (py_object.ptr ()) || PyUnicode_Check (py_object.ptr ())) |
352 oct_value = extract_py_str (py_object.ptr ()); | 331 oct_value = extract_py_str (py_object.ptr ()); |
353 else | 332 else |
354 pyobj_to_oct_pyobject (oct_value, py_object); | 333 oct_value = pyobject_wrap_object (py_object.ptr ()); |
355 } | 334 } |
356 | 335 |
357 void pytuple_to_octlist (octave_value_list& octave_list, | 336 void pytuple_to_octlist (octave_value_list& octave_list, |
358 const boost::python::tuple& python_tuple) | 337 const boost::python::tuple& python_tuple) |
359 { | 338 { |