Skip to content

Commit

Permalink
App: Set PropertyMap using Python objects with mapping protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
marioalexis84 committed Jan 5, 2025
1 parent 26536c9 commit d58f425
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/App/PropertyStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1926,25 +1926,22 @@ PyObject* PropertyMap::getPyObject()

void PropertyMap::setPyObject(PyObject* value)
{
if (PyDict_Check(value)) {

if (PyMapping_Check(value)) {
std::map<std::string, std::string> values;
// get key and item list
PyObject* keyList = PyDict_Keys(value);

PyObject* itemList = PyDict_Values(value);
PyObject* keyList = PyMapping_Keys(value);
PyObject* itemList = PyMapping_Values(value);
Py_ssize_t nSize = PyList_Size(keyList);

for (Py_ssize_t i = 0; i < nSize; ++i) {

// check on the key:
std::string keyStr;
PyObject* key = PyList_GetItem(keyList, i);
if (PyUnicode_Check(key)) {
keyStr = PyUnicode_AsUTF8(key);
}
else {
std::string error("type of the key need to be unicode or string, not");
std::string error("type of the key need to be string, not ");
error += key->ob_type->tp_name;
throw Base::TypeError(error);
}
Expand All @@ -1955,16 +1952,19 @@ void PropertyMap::setPyObject(PyObject* value)
values[keyStr] = PyUnicode_AsUTF8(item);
}
else {
std::string error("type in list must be string or unicode, not ");
std::string error("type in values must be string, not ");
error += item->ob_type->tp_name;
throw Base::TypeError(error);
}
}

Py_XDECREF(itemList);
Py_XDECREF(keyList);

setValues(values);
}
else {
std::string error("type must be a dict object");
std::string error("type must be a dict or object with mapping protocol, not ");
error += value->ob_type->tp_name;
throw Base::TypeError(error);
}
Expand Down

0 comments on commit d58f425

Please sign in to comment.