Make Oid hashable

This commit is contained in:
J. David Ibáñez
2013-04-18 23:57:19 +02:00
parent f74a211f14
commit 4cf1fc2371
3 changed files with 24 additions and 1 deletions

View File

@@ -200,6 +200,14 @@ Oid_init(Oid *self, PyObject *args, PyObject *kw)
} }
Py_hash_t
Oid_hash(PyObject *oid)
{
/* TODO Randomize (use _Py_HashSecret) to avoid collission DoS attacks? */
return *(Py_hash_t*) ((Oid*)oid)->oid.id;
}
PyObject * PyObject *
Oid_richcompare(PyObject *o1, PyObject *o2, int op) Oid_richcompare(PyObject *o1, PyObject *o2, int op)
{ {
@@ -279,7 +287,7 @@ PyTypeObject OidType = {
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ (hashfunc)Oid_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */

View File

@@ -54,6 +54,11 @@
#define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict") #define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict")
#endif #endif
#ifndef Py_hash_t
#define Py_hash_t long
#endif
#define CHECK_REFERENCE(self)\ #define CHECK_REFERENCE(self)\
if (self->reference == NULL) {\ if (self->reference == NULL) {\
PyErr_SetString(GitError, "deleted reference");\ PyErr_SetString(GitError, "deleted reference");\

View File

@@ -94,6 +94,16 @@ class OidTest(utils.BareRepoTestCase):
self.assertFalse(oid1 > oid2) self.assertFalse(oid1 > oid2)
self.assertFalse(oid1 >= oid2) self.assertFalse(oid1 >= oid2)
def test_hash(self):
s = set()
s.add(Oid(raw=RAW))
s.add(Oid(hex=HEX))
self.assertEqual(len(s), 1)
s.add(Oid(hex="0000000000000000000000000000000000000000"))
s.add(Oid(hex="0000000000000000000000000000000000000001"))
self.assertEqual(len(s), 3)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()