Mercurial > pytave
changeset 342:6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
* oct-py-types.cc, oct-py-types.h (pytave::make_py_tuple): Rename from
pytave::make_py_list, construct a tuple instead of list.
* octave_to_python.cc (pytave::octvalue_to_pyobj): Call make_py_tuple.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Tue, 16 Aug 2016 12:57:07 -0700 |
parents | b0d012e9975f |
children | fe6b9e618c98 |
files | oct-py-types.cc oct-py-types.h octave_to_python.cc |
diffstat | 3 files changed, 12 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/oct-py-types.cc Tue Aug 16 00:31:22 2016 -0700 +++ b/oct-py-types.cc Tue Aug 16 12:57:07 2016 -0700 @@ -219,25 +219,24 @@ } PyObject * -make_py_list (const Cell& cell) +make_py_tuple (const Cell& cell) { if (! (cell.is_empty () || cell.is_vector ())) throw value_convert_exception ( - "unable to convert multidimensional cell array into Python sequence"); + "unable to convert multidimensional cell array into Python tuple"); - PyObject *list = PyList_New (0); - if (! list) + octave_idx_type size = cell.numel (); + PyObject *tuple = PyTuple_New (size); + if (! tuple) octave_throw_bad_alloc (); - for (octave_idx_type i = 0; i < cell.numel (); i++) + for (octave_idx_type i = 0; i < size; ++i) { PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i)); - - if (PyList_Append (list, item) < 0) - throw boost::python::error_already_set (); + PyTuple_SET_ITEM (tuple, i, item); } - return list; + return tuple; } std::string
--- a/oct-py-types.h Tue Aug 16 00:31:22 2016 -0700 +++ b/oct-py-types.h Tue Aug 16 12:57:07 2016 -0700 @@ -121,15 +121,15 @@ PyObject * make_py_int (uint64_t value); -//! Create a Python list object from the given Octave cell array value. +//! Create a Python tuple object from the given Octave cell array value. //! //! The values contained in the cell array are recursively converted to //! appropriate Python values. //! //! @param cell Octave cell array -//! @return Python list object +//! @return Python tuple object PyObject * -make_py_list (const Cell& cell); +make_py_tuple (const Cell& cell); //! Create a Python numeric object from the given Octave numeric or boolean //! scalar value.
--- a/octave_to_python.cc Tue Aug 16 00:31:22 2016 -0700 +++ b/octave_to_python.cc Tue Aug 16 12:57:07 2016 -0700 @@ -175,7 +175,7 @@ } else if (octvalue.is_cell ()) { - PyObject *obj = make_py_list (octvalue.cell_value ()); + PyObject *obj = make_py_tuple (octvalue.cell_value ()); py_object = object (handle<PyObject> (obj)); } else if (octvalue.is_numeric_type () || octvalue.is_string ()