changeset 185:cede17a86ccf

Convert Octave char arrays into Python strings (fixes issue #7) * octave_to_python.cc (octstring_to_pyobject): New function to convert std::string into Python string. (octvalue_to_pyobj): Call it when value is a char array.
author Mike Miller <mtmiller@octave.org>
date Wed, 08 Jun 2016 15:56:25 -0700
parents 8b97647e48f1
children 277b1e172123
files octave_to_python.cc
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/octave_to_python.cc	Wed Jun 08 12:24:59 2016 -0700
+++ b/octave_to_python.cc	Wed Jun 08 15:56:25 2016 -0700
@@ -210,12 +210,27 @@
       }
   }
 
+  static void
+  octstring_to_pyobject (boost::python::object& py_object,
+                         const std::string& str)
+  {
+    py_object = object (handle<PyObject> (
+#if PY_VERSION_HEX >= 0x03000000
+      PyUnicode_FromStringAndSize (str.data (), str.size ())
+#else
+      PyString_FromStringAndSize (str.data (), str.size ())
+#endif
+    ));
+  }
+
   void octvalue_to_pyobj (boost::python::object& py_object,
                           const octave_value& octvalue)
   {
     if (octvalue.is_undefined ())
       throw value_convert_exception (
         "Octave value `undefined'. Can not convert to a Python object");
+    else if (octvalue.is_string ())
+      octstring_to_pyobject (py_object, octvalue.string_value ());
     else if (octvalue.is_numeric_type () || octvalue.is_string ()
              || octvalue.is_cell () || octvalue.is_bool_type ())
       octvalue_to_pyarr (py_object, octvalue);