aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2012-05-02 21:45:52 -0300
committerHugo Parente Lima <hugo.lima@openbossa.org>2012-05-03 03:13:50 +0200
commit54cce10fa8a9942450c9e1a9d9a9d2a1b688f243 (patch)
treeb15e041fc2a891846f38565c655c32a2546e7be3
parent9a8eaf81a4694088cb2a5f3fcea753ea588cb7cf (diff)
Set a default hash function for all ObjectTypes.
Fix bug PYSIDE-42 Change-Id: I1392374dcf0055309152082e674cc1e3e6472d4d Reviewed-by: Paulo Alcantara <paulo.alcantara@openbossa.org> Reviewed-by: Hugo Parente Lima <hugo.lima@openbossa.org>
-rw-r--r--generator/shiboken/cppgenerator.cpp2
-rw-r--r--libshiboken/basewrapper.cpp6
-rw-r--r--libshiboken/basewrapper.h5
-rw-r--r--tests/otherbinding/objtypehashes_test.py17
4 files changed, 30 insertions, 0 deletions
diff --git a/generator/shiboken/cppgenerator.cpp b/generator/shiboken/cppgenerator.cpp
index fa19f7a3d..32fcdec7e 100644
--- a/generator/shiboken/cppgenerator.cpp
+++ b/generator/shiboken/cppgenerator.cpp
@@ -3385,6 +3385,8 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass*
if (!metaClass->typeEntry()->hashFunction().isEmpty())
tp_hash = '&' + cpythonBaseName(metaClass) + "_HashFunc";
+ else if (isObjectType(metaClass))
+ tp_hash = "&Shiboken::Object::hash";
const AbstractMetaFunction* callOp = metaClass->findFunction("operator()");
if (callOp && !callOp->isModifiedRemoved())
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 0d2d4b6e5..0b7617e5c 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -748,6 +748,12 @@ bool isUserType(PyObject* pyObj)
return ObjectType::isUserType(pyObj->ob_type);
}
+Py_hash_t hash(PyObject* pyObj)
+{
+ assert(Shiboken::Object::checkType(pyObj));
+ return reinterpret_cast<Py_hash_t>(reinterpret_cast<SbkObject*>(pyObj)->d->cptr[0]);
+}
+
static void setSequenceOwnership(PyObject* pyObj, bool owner)
{
if (PySequence_Check(pyObj)) {
diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h
index e9a94a36a..a3d61b88e 100644
--- a/libshiboken/basewrapper.h
+++ b/libshiboken/basewrapper.h
@@ -231,6 +231,11 @@ LIBSHIBOKEN_API bool checkType(PyObject* pyObj);
LIBSHIBOKEN_API bool isUserType(PyObject* pyObj);
/**
+ * Generic function used to make ObjectType hashable, the C++ pointer is used as hash value.
+ */
+LIBSHIBOKEN_API Py_hash_t hash(PyObject* pyObj);
+
+/**
* Bind a C++ object to Python.
* \param instanceType equivalent Python type for the C++ object.
* \param hasOwnership if true, Python will try to delete the underlying C++ object when there's no more refs.
diff --git a/tests/otherbinding/objtypehashes_test.py b/tests/otherbinding/objtypehashes_test.py
new file mode 100644
index 000000000..37ae9b85c
--- /dev/null
+++ b/tests/otherbinding/objtypehashes_test.py
@@ -0,0 +1,17 @@
+import unittest
+from sample import *
+from other import *
+
+class TestHashFuncs (unittest.TestCase):
+
+ def testIt(self):
+ obj1 = HandleHolder()
+ obj2 = HandleHolder()
+
+ hash1 = hash(obj1)
+ hash2 = hash(obj2)
+ self.assertNotEqual(hash1, hash2)
+ self.assertNotEqual(hash(HandleHolder()), hash(HandleHolder()))
+
+if __name__ == '__main__':
+ unittest.main()