From: JINMEI Tatuya Date: Mon, 2 Jul 2012 23:11:33 +0000 (-0700) Subject: [1883] [res-research] added trivial tp_hash for isc.dns.RRType X-Git-Tag: trac2351_base~117^2~36^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e85f1ea840dcd93f80f154600d762b030888f8f;p=thirdparty%2Fkea.git [1883] [res-research] added trivial tp_hash for isc.dns.RRType --- diff --git a/src/lib/dns/python/rrtype_python.cc b/src/lib/dns/python/rrtype_python.cc index bf20b7cd9e..bf22e8d374 100644 --- a/src/lib/dns/python/rrtype_python.cc +++ b/src/lib/dns/python/rrtype_python.cc @@ -49,6 +49,7 @@ PyObject* RRType_str(PyObject* self); PyObject* RRType_toWire(s_RRType* self, PyObject* args); PyObject* RRType_getCode(s_RRType* self); PyObject* RRType_richcmp(s_RRType* self, s_RRType* other, int op); +Py_hash_t RRType_hash(PyObject* pyself); PyObject* RRType_NSEC3PARAM(s_RRType *self); PyObject* RRType_DNAME(s_RRType *self); PyObject* RRType_PTR(s_RRType *self); @@ -368,6 +369,11 @@ RRType_ANY(s_RRType*) { return (RRType_createStatic(RRType::ANY())); } +Py_hash_t +RRType_hash(PyObject* pyself) { + s_RRType* const self = static_cast(pyself); + return (self->cppobj->getCode()); +} } // end anonymous namespace namespace isc { @@ -394,7 +400,7 @@ PyTypeObject rrtype_type = { NULL, // tp_as_number NULL, // tp_as_sequence NULL, // tp_as_mapping - NULL, // tp_hash + RRType_hash, // tp_hash NULL, // tp_call RRType_str, // tp_str NULL, // tp_getattro diff --git a/src/lib/dns/python/tests/rrtype_python_test.py b/src/lib/dns/python/tests/rrtype_python_test.py index 713542653a..fde9c7417a 100644 --- a/src/lib/dns/python/tests/rrtype_python_test.py +++ b/src/lib/dns/python/tests/rrtype_python_test.py @@ -116,6 +116,14 @@ class TestModuleSpec(unittest.TestCase): self.assertFalse(self.rrtype_1 == 1) + def test_hash(self): + # Exploiting the knowledge that the hash value is the numeric class + # value, we can predict the comparison result. + self.assertEqual(hash(RRType.AAAA()), hash(RRType("AAAA"))) + self.assertEqual(hash(RRType("aaaa")), hash(RRType("AAAA"))) + self.assertNotEqual(hash(RRType.A()), hash(RRType.NS())) + self.assertNotEqual(hash(RRType.AAAA()), hash(RRType("Type65535"))) + def test_statics(self): self.assertEqual(RRType("NSEC3PARAM"), RRType.NSEC3PARAM()) self.assertEqual(RRType("DNAME"), RRType.DNAME())