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 13, 2018
1 parent ba819e6 commit 1807e41
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 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 @@ -386,30 +386,52 @@ static bool string_from_strpy(SGStringList<type>& sg_strings, PyObject* obj, int
/* Check if is a list */
if (!list || PyList_Check(list) || PyList_Size(list)==0)
{
int32_t size=PyList_Size(list);
int64_t size=PyList_Size(list);
shogun::SGString<type>* strings=SG_MALLOC(shogun::SGString<type>, size);

int32_t max_len=0;
for (int32_t i=0; i<size; ++i)
int64_t max_len=0;
for (auto i=0; i<size; ++i)
{
PyObject *o = PyList_GetItem(list,i);
if (typecode == NPY_STRING || typecode == NPY_UNICODE)
{
#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)));
int64_t len = -1;
const char* str = PyUnicode_AsUTF8AndSize(o, &len);
#else
int32_t len = PyString_Size(o);
const char* str = PyString_AsString(o);
int64_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
max_len=shogun::CMath::max(len,max_len);
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;
strings[i].string=NULL;
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 All @@ -440,7 +463,7 @@ static bool string_from_strpy(SGStringList<type>& sg_strings, PyObject* obj, int
return false;

type* str=(type*) PyArray_DATA(array);
int32_t len = PyArray_DIM(array,0);
int64_t len = PyArray_DIM(array,0);
max_len=shogun::CMath::max(len,max_len);

strings[i].slen=len;
Expand Down

0 comments on commit 1807e41

Please sign in to comment.