Make Oid hashable
This commit is contained in:
10
src/oid.c
10
src/oid.c
@@ -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 *
|
||||
Oid_richcompare(PyObject *o1, PyObject *o2, int op)
|
||||
{
|
||||
@@ -279,7 +287,7 @@ PyTypeObject OidType = {
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
(hashfunc)Oid_hash, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
|
@@ -54,6 +54,11 @@
|
||||
#define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict")
|
||||
#endif
|
||||
|
||||
#ifndef Py_hash_t
|
||||
#define Py_hash_t long
|
||||
#endif
|
||||
|
||||
|
||||
#define CHECK_REFERENCE(self)\
|
||||
if (self->reference == NULL) {\
|
||||
PyErr_SetString(GitError, "deleted reference");\
|
||||
|
@@ -94,6 +94,16 @@ class OidTest(utils.BareRepoTestCase):
|
||||
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__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user