diff options
author | Hugo Parente Lima <hugo.pl@gmail.com> | 2011-10-14 15:24:13 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:18:22 -0300 |
commit | 980f4119e332f6eff936b5061bb9a2bc28923e89 (patch) | |
tree | b6d11f4ac3d9987bd616d2f7bb79536b51c586d8 | |
parent | 0b1b29bf42271754657ec6b73471f59b38f9e462 (diff) |
Fixed regression that made enums not hashable.
-rw-r--r-- | libshiboken/sbkenum.cpp | 6 | ||||
-rw-r--r-- | libshiboken/sbkpython.h | 2 | ||||
-rw-r--r-- | tests/samplebinding/enum_test.py | 3 |
3 files changed, 10 insertions, 1 deletions
diff --git a/libshiboken/sbkenum.cpp b/libshiboken/sbkenum.cpp index 0304a1a9e..e6da02d7f 100644 --- a/libshiboken/sbkenum.cpp +++ b/libshiboken/sbkenum.cpp @@ -230,7 +230,10 @@ enum_richcompare(PyObject *self, PyObject *other, int op) Py_RETURN_FALSE; } - +static Py_hash_t enum_hash(PyObject* pyObj) +{ + return PyObject_Hash(reinterpret_cast<SbkEnumObject*>(pyObj)->ob_name); +} static PyGetSetDef SbkEnumGetSetList[] = { {const_cast<char*>("name"), &SbkEnumObject_name}, @@ -507,6 +510,7 @@ PyTypeObject* newTypeWithName(const char* name, const char* cppName) type->tp_new = SbkEnum_tp_new; type->tp_as_number = &enum_as_number; type->tp_richcompare = &enum_richcompare; + type->tp_hash = &enum_hash; DeclaredEnumTypes::instance().addEnumType(type, cppName); return type; diff --git a/libshiboken/sbkpython.h b/libshiboken/sbkpython.h index d269c7e98..850f122ff 100644 --- a/libshiboken/sbkpython.h +++ b/libshiboken/sbkpython.h @@ -49,6 +49,8 @@ #define SBK_NB_BOOL(x) (x).nb_nonzero #define SBK_STR_NAME "str" #define SBK_PyMethod_New(X, Y) PyMethod_New(X, Y, (PyObject*)Py_TYPE(Y)) + + #define Py_hash_t long #endif #endif diff --git a/tests/samplebinding/enum_test.py b/tests/samplebinding/enum_test.py index fefe75ce3..6b3609a39 100644 --- a/tests/samplebinding/enum_test.py +++ b/tests/samplebinding/enum_test.py @@ -51,6 +51,9 @@ class EnumTest(unittest.TestCase): enum = SampleNamespace.Option(999) self.assertEqual(eval(repr(enum)), enum) + def testHashability(self): + '''Enums should be hashable and different enums with different values should have different hashes''' + self.assertNotEqual(hash(SampleNamespace.TwoIn), hash(SampleNamespace.TwoOut)) def testEnumValuesInsideEnum(self): '''Enum values should be accessible inside the enum as well as outside.''' |