diff --git a/src/interfaces/python/swig_typemaps.i b/src/interfaces/python/swig_typemaps.i index b66e36844bd..03928610164 100644 --- a/src/interfaces/python/swig_typemaps.i +++ b/src/interfaces/python/swig_typemaps.i @@ -211,7 +211,7 @@ static int is_pystring_list(PyObject* obj, int typecode) #if PY_VERSION_HEX >= 0x03000000 if (!PyUnicode_Check(o)) #else - if (!PyString_Check(o)) + if (!PyString_Check(o) && !PyUnicode_Check(o)) #endif { result=0; @@ -398,17 +398,39 @@ static bool string_from_strpy(SGStringList& sg_strings, PyObject* obj, int #if PY_VERSION_HEX >= 0x03000000 if (PyUnicode_Check(o)) #else - if (PyString_Check(o)) + if (PyString_Check(o) || PyUnicode_Check(o)) #endif { - + PyObject *tmp = nullptr; #if PY_VERSION_HEX >= 0x03000000 - int32_t len = PyUnicode_GetSize((PyObject*) o); - const char* str = PyBytes_AsString(PyUnicode_AsASCIIString(const_cast(o))); + index_t len = -1; + const char* str = PyUnicode_AsUTF8AndSize(o, &len); #else - int32_t len = PyString_Size(o); - const char* str = PyString_AsString(o); + index_t len = -1; + const char* str = nullptr; + if (PyString_Check(o)) + { + len = PyString_Size(o); + str = PyString_AsString(o); + } + else + { + tmp = PyUnicode_AsUTF8String(o); + if (tmp != nullptr) + { + str = PyString_AsString(tmp); + len = PyUnicode_GetSize(o); + } + } #endif + if (str == nullptr) + { + PyErr_SetString(PyExc_TypeError, "Error converting string content."); + for (auto j=0; j& sg_strings, PyObject* obj, int { strings[i].string=SG_MALLOC(type, len); sg_memcpy(strings[i].string, str, len); + Py_XDECREF(tmp); } } else { PyErr_SetString(PyExc_TypeError, "all elements in list must be strings"); - for (int32_t j=0; j