summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libshiboken/sbkenum.cpp41
-rw-r--r--libshiboken/sbkenum.h1
2 files changed, 33 insertions, 9 deletions
diff --git a/libshiboken/sbkenum.cpp b/libshiboken/sbkenum.cpp
index 7aea88ad..5ae95ad7 100644
--- a/libshiboken/sbkenum.cpp
+++ b/libshiboken/sbkenum.cpp
@@ -149,21 +149,44 @@ private:
namespace Enum {
+PyObject* getEnumItemFromValue(PyTypeObject* enumType, long itemValue)
+{
+ PyObject *key, *value;
+ Py_ssize_t pos = 0;
+ PyObject* values = PyDict_GetItemString(enumType->tp_dict, const_cast<char*>("values"));
+
+ while (PyDict_Next(values, &pos, &key, &value)) {
+ SbkEnumObject* obj = (SbkEnumObject*)value;
+ if (obj->ob_ival == itemValue) {
+ Py_INCREF(obj);
+ return reinterpret_cast<PyObject*>(obj);
+ }
+ }
+ return 0;
+}
+
PyObject* newItem(PyTypeObject* enumType, long itemValue, const char* itemName)
{
- if (!itemName)
- itemName = "";
- PyObject* pyItemName = PyString_FromString(itemName);
+ bool newValue = true;
+ SbkEnumObject* enumObj;
+ if (!itemName) {
+ enumObj = reinterpret_cast<SbkEnumObject*>(getEnumItemFromValue(enumType, itemValue));
+ if (enumObj)
+ return reinterpret_cast<PyObject*>(enumObj);
+
+ newValue = false;
+ if (!enumObj)
+ itemName = "#out of bounds#";
+ }
- SbkEnumObject* enumObj = PyObject_New(SbkEnumObject, enumType);
- if (!enumObj) {
- Py_XDECREF(pyItemName);
+ enumObj = PyObject_New(SbkEnumObject, enumType);
+ if (!enumObj)
return 0;
- }
- enumObj->ob_name = pyItemName;
+ enumObj->ob_name = PyString_FromString(itemName);
enumObj->ob_ival = itemValue;
- if (itemName) {
+
+ if (newValue) {
PyObject* values = PyDict_GetItemString(enumType->tp_dict, const_cast<char*>("values"));
if (!values) {
values = PyDict_New();
diff --git a/libshiboken/sbkenum.h b/libshiboken/sbkenum.h
index 50ac9220..95b500ca 100644
--- a/libshiboken/sbkenum.h
+++ b/libshiboken/sbkenum.h
@@ -46,6 +46,7 @@ namespace Enum
LIBSHIBOKEN_API PyObject* newItem(PyTypeObject* enumType, long itemValue, const char* itemName = 0);
LIBSHIBOKEN_API PyTypeObject* newType(const char* name);
LIBSHIBOKEN_API long getValue(PyObject* enumItem);
+ LIBSHIBOKEN_API PyObject* getEnumItemFromValue(PyTypeObject* enumType, long itemValue);
}
} // namespace Shiboken