Mercurial > pytave
changeset 177:c956f7a932e7
Convert Python unicode strings into UTF-8 char arrays (fixes issue #5)
* python_to_octave.cc (pyunicode_to_utf8): New function to encode unicode
string to UTF-8 std::string. (pyobj_to_octvalue): Use it.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 26 May 2016 18:24:55 -0700 |
parents | 72fa08eb9681 |
children | 9aeb3cd4a288 |
files | pycall.cc python_to_octave.cc |
diffstat | 2 files changed, 18 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/pycall.cc Fri May 13 15:55:24 2016 -0700 +++ b/pycall.cc Thu May 26 18:24:55 2016 -0700 @@ -86,8 +86,11 @@ try { - object main_module = import ("__main__"); - object main_namespace = main_module.attr ("__dict__"); +#if PY_VERSION_HEX >= 0x03000000 + object main_module = import ("builtins"); +#else + object main_module = import ("__builtin__"); +#endif object mod = (module.empty ()) ? main_module : import (module.c_str ()); object callable = mod.attr (func.c_str ());
--- a/python_to_octave.cc Fri May 13 15:55:24 2016 -0700 +++ b/python_to_octave.cc Thu May 26 18:24:55 2016 -0700 @@ -446,6 +446,16 @@ oct_value = map; } + static std::string + pyunicode_to_utf8 (PyObject *unicode) + { + std::string str; + PyObject *utf8 = PyUnicode_AsUTF8String (unicode); + str.assign (PyBytes_AsString (utf8), PyBytes_Size (utf8)); + Py_DECREF (utf8); + return str; + } + void pyobj_to_octvalue (octave_value& oct_value, const boost::python::object& py_object) { @@ -453,6 +463,7 @@ extract<double> doublex (py_object); extract<Complex> complexx (py_object); extract<std::string> stringx (py_object); + extract<std::wstring> wstringx (py_object); extract<numeric::array> arrayx (py_object); extract<boost::python::list> listx (py_object); extract<boost::python::dict> dictx (py_object); @@ -467,6 +478,8 @@ pyarr_to_octvalue (oct_value, (PyArrayObject*)py_object.ptr ()); else if (stringx.check ()) oct_value = stringx (); + else if (wstringx.check ()) + oct_value = pyunicode_to_utf8 (py_object.ptr ()); else if (listx.check ()) pylist_to_cellarray (oct_value, (boost::python::list&)py_object); else if (dictx.check ())