# HG changeset patch # User Mike Miller # Date 1471302878 25200 # Node ID c081e30c2f64bf025854a3df2ce1f8b1b3aff33e # Parent 9b862844e6b7d143ac1da584702340ea83e21c95 Overhaul Python list creation from cell array * oct-py-types.cc, oct-py-types.h (pytave::make_py_list): New function. * octave_to_python.cc (pytave::octvalue_to_pyobj): Use it. (pytave::octcell_to_pyobject): Delete. diff -r 9b862844e6b7 -r c081e30c2f64 oct-py-types.cc --- a/oct-py-types.cc Mon Aug 15 15:16:24 2016 -0700 +++ b/oct-py-types.cc Mon Aug 15 16:14:38 2016 -0700 @@ -24,6 +24,7 @@ # include #endif +#include #include #include @@ -86,6 +87,28 @@ return 0; } +PyObject * +make_py_list (const Cell& cell) +{ + if (! (cell.is_empty () || cell.is_vector ())) + throw value_convert_exception ( + "unable to convert multidimensional cell array into Python sequence"); + + PyObject *list = PyList_New (0); + if (! list) + octave_throw_bad_alloc (); + + for (octave_idx_type i = 0; i < cell.numel (); i++) + { + PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i)); + + if (PyList_Append (list, item) < 0) + throw boost::python::error_already_set (); + } + + return list; +} + std::string extract_py_str (PyObject *obj) { diff -r 9b862844e6b7 -r c081e30c2f64 oct-py-types.h --- a/oct-py-types.h Mon Aug 15 15:16:24 2016 -0700 +++ b/oct-py-types.h Mon Aug 15 16:14:38 2016 -0700 @@ -26,6 +26,7 @@ #include #include +class Cell; class octave_scalar_map; namespace pytave @@ -37,6 +38,9 @@ int64_t extract_py_int64 (PyObject *obj); +PyObject * +make_py_list (const Cell& cell); + std::string extract_py_str (PyObject *obj); diff -r 9b862844e6b7 -r c081e30c2f64 octave_to_python.cc --- a/octave_to_python.cc Mon Aug 15 15:16:24 2016 -0700 +++ b/octave_to_python.cc Mon Aug 15 16:14:38 2016 -0700 @@ -157,26 +157,6 @@ py_object = object (handle ((PyObject *)pyarr)); } - static void - octcell_to_pyobject (boost::python::object& py_object, - const Cell& cell) - { - if (! (cell.is_empty () || cell.is_vector ())) - throw value_convert_exception ( - "unable to convert multidimensional cell array into Python sequence"); - - boost::python::list sequence; - - for (octave_idx_type i = 0; i < cell.numel (); i++) - { - boost::python::object py_val; - octvalue_to_pyobj (py_val, cell(i)); - sequence.append (py_val); - } - - py_object = sequence; - } - inline PyObject * python_integer_value (int32_t value) { @@ -266,7 +246,10 @@ else if (octvalue.is_scalar_type ()) octscalar_to_pyobject (py_object, octvalue); else if (octvalue.is_cell ()) - octcell_to_pyobject (py_object, octvalue.cell_value ()); + { + PyObject *obj = make_py_list (octvalue.cell_value ()); + py_object = object (handle (obj)); + } else if (octvalue.is_numeric_type () || octvalue.is_string () || octvalue.is_bool_type ()) octvalue_to_pyarr (py_object, octvalue);