summaryrefslogtreecommitdiffstats
path: root/libshiboken/sbkenum.cpp
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.filho@openbossa.org>2011-01-28 11:57:21 -0300
committerRenato Araujo Oliveira Filho <renato.filho@openbossa.org>2011-01-28 19:30:08 -0300
commit9c2d358942798ef6e5ef5260d15c889e9fac95ee (patch)
tree5b0d0af7ff0734ecc829211c94f0c2b8f99dfc2a /libshiboken/sbkenum.cpp
parent6674f8a265e882fbb1c381c9a7f8ed6e61807dc2 (diff)
downloadshiboken-9c2d358942798ef6e5ef5260d15c889e9fac95ee.tar.gz
shiboken-9c2d358942798ef6e5ef5260d15c889e9fac95ee.tar.xz
shiboken-9c2d358942798ef6e5ef5260d15c889e9fac95ee.zip
Fixed enum __repr__ function.
fixes bug #617. Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'libshiboken/sbkenum.cpp')
-rw-r--r--libshiboken/sbkenum.cpp41
1 files changed, 32 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();