changeset 336:c081e30c2f64

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.
author Mike Miller <mtmiller@octave.org>
date Mon, 15 Aug 2016 16:14:38 -0700
parents 9b862844e6b7
children d3d355dc44ad
files oct-py-types.cc oct-py-types.h octave_to_python.cc
diffstat 3 files changed, 31 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- 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 <config.h>
 #endif
 
+#include <octave/Cell.h>
 #include <octave/oct-map.h>
 #include <octave/quit.h>
 
@@ -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)
 {
--- 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 <Python.h>
 #include <string>
 
+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);
 
--- 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> ((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<PyObject> (obj));
+      }
     else if (octvalue.is_numeric_type () || octvalue.is_string ()
              || octvalue.is_bool_type ())
       octvalue_to_pyarr (py_object, octvalue);