]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[1883] [res-research] added trivial tp_hash for isc.dns.RRType
authorJINMEI Tatuya <jinmei@isc.org>
Mon, 2 Jul 2012 23:11:33 +0000 (16:11 -0700)
committerJINMEI Tatuya <jinmei@isc.org>
Mon, 2 Jul 2012 23:13:23 +0000 (16:13 -0700)
src/lib/dns/python/rrtype_python.cc
src/lib/dns/python/tests/rrtype_python_test.py

index bf20b7cd9edc5d534c310cd9f76d152d65ddfc8b..bf22e8d374f8666c6d2f0c84d52d65b1314fbf01 100644 (file)
@@ -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<s_RRType*>(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
index 713542653aa1f003c9b705dad2e1e4307675cbfd..fde9c7417adbdcb5589efc60eb5fb025c969415d 100644 (file)
@@ -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())