Skip to content

Commit

Permalink
[PYTHON] Support unicode string list in python #4157
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Feb 12, 2018
1 parent ba819e6 commit ea14003
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/interfaces/python/swig_typemaps.i
Expand Up @@ -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;
Expand Down Expand Up @@ -398,17 +398,39 @@ static bool string_from_strpy(SGStringList<type>& 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<PyObject*>(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<i; ++j)
SG_FREE(strings[i].string);
SG_FREE(strings);
return false;
}
max_len=shogun::CMath::max(len,max_len);

strings[i].slen=len;
Expand All @@ -418,13 +440,14 @@ static bool string_from_strpy(SGStringList<type>& 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<i; ++j)
for (auto j=0; j<i; ++j)
SG_FREE(strings[i].string);
SG_FREE(strings);
return false;
Expand Down

0 comments on commit ea14003

Please sign in to comment.