improve unicode support for enumeration properties

This commit is contained in:
wmayer
2016-11-14 15:22:21 +01:00
parent 425e12ce18
commit 88485684a5
2 changed files with 35 additions and 13 deletions

View File

@@ -441,27 +441,48 @@ void PropertyEnumeration::setPyObject(PyObject *value)
throw Base::ValueError(out.str());
}
}
else if (PyList_Check(value)) {
Py_ssize_t nSize = PyList_Size(value);
else if (PyUnicode_Check(value)) {
PyObject* unicode = PyUnicode_AsUTF8String(value);
std::string str = PyString_AsString(unicode);
Py_DECREF(unicode);
if (_enum.contains(str.c_str())) {
aboutToSetValue();
_enum.setValue(str);
hasSetValue();
}
else {
std::stringstream out;
out << "'" << str << "' is not part of the enumeration";
throw Base::ValueError(out.str());
}
}
else if (PySequence_Check(value)) {
Py_ssize_t nSize = PySequence_Size(value);
std::vector<std::string> values;
values.resize(nSize);
for (Py_ssize_t i = 0; i < nSize; ++i) {
PyObject *item = PyList_GetItem(value, i);
PyObject *item = PySequence_GetItem(value, i);
if ( !PyString_Check(item) ) {
std::string error = std::string("type in list must be str, not ");
if (PyString_Check(item)) {
values[i] = PyString_AsString(item);
}
else if (PyUnicode_Check(item)) {
PyObject* unicode = PyUnicode_AsUTF8String(item);
values[i] = PyString_AsString(unicode);
Py_DECREF(unicode);
}
else {
std::string error = std::string("type in list must be str or unicode, not ");
throw Base::TypeError(error + item->ob_type->tp_name);
}
values[i] = PyString_AsString(item);
}
_enum.setEnums(values);
setValue((long)0);
}
else {
std::string error = std::string("type must be int or str, not ");
std::string error = std::string("type must be int, str or unicode not ");
throw Base::TypeError(error + value->ob_type->tp_name);
}
}