Browse Source

use c++ style casts. Most of them are reinterpret_casts because of the way we used the strucutres in c-style as well, will change in next commit

git-svn-id: svn://bind10.isc.org/svn/bind10/experiments/python-binding@2257 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
9cf1246826

+ 45 - 1
src/lib/dns/python/rdata_python.cc

@@ -60,6 +60,7 @@ static PyObject* Rdata_toText(s_Rdata* self);
 // is a PyObject*, for the str() function in python.
 static PyObject* Rdata_str(PyObject* self);
 static PyObject* Rdata_toWire(s_Rdata* self, PyObject* args);
+static PyObject* RData_richcmp(s_Rdata* self, s_Rdata* other, int op);
 
 // This list contains the actual set of functions we have in
 // python. Each entry has
@@ -108,7 +109,7 @@ static PyTypeObject rdata_type = {
     "a set of common interfaces to manipulate concrete RDATA objects.",
     NULL,                               // tp_traverse
     NULL,                               // tp_clear
-    NULL,                               // tp_richcompare
+    (richcmpfunc)RData_richcmp,         // tp_richcompare
     0,                                  // tp_weaklistoffset
     NULL,                               // tp_iter
     NULL,                               // tp_iternext
@@ -202,6 +203,49 @@ Rdata_toWire(s_Rdata* self, PyObject* args) {
     return NULL;
 }
 
+
+
+static PyObject* 
+RData_richcmp(s_Rdata* self, s_Rdata* other, int op) {
+    bool c;
+
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other || (self->ob_type != other->ob_type)) {
+        Py_RETURN_FALSE;
+    }
+
+    switch (op) {
+    case Py_LT:
+        c = self->rdata->compare(*other->rdata) < 0;
+        break;
+    case Py_LE:
+        c = self->rdata->compare(*other->rdata) < 0 ||
+            self->rdata->compare(*other->rdata) == 0;
+        break;
+    case Py_EQ:
+        c = self->rdata->compare(*other->rdata) == 0;
+        break;
+    case Py_NE:
+        c = self->rdata->compare(*other->rdata) != 0;
+        break;
+    case Py_GT:
+        c = self->rdata->compare(*other->rdata) > 0;
+        break;
+    case Py_GE:
+        c = self->rdata->compare(*other->rdata) > 0 ||
+            self->rdata->compare(*other->rdata) == 0;
+        break;
+    default:
+        PyErr_SetString(PyExc_IndexError,
+                        "Unhandled rich comparison operator");
+        return NULL;
+    }
+    if (c)
+        Py_RETURN_TRUE;
+    else
+        Py_RETURN_FALSE;
+}
 // end of Rdata
 
 

+ 26 - 48
src/lib/dns/python/rrtype_python.cc

@@ -57,52 +57,29 @@ static PyObject*
 RRType_toText(s_RRType* self);
 // This is a second version of toText, we need one where the argument
 // is a PyObject*, for the str() function in python.
-static PyObject*
-RRType_str(PyObject* self);
-static PyObject*
-RRType_toWire(s_RRType* self, PyObject* args);
-static PyObject*
-RRType_getCode(s_RRType* self);
-static PyObject*
-RRType_richcmp(s_RRType* self, s_RRType* other, int op);
-static PyObject*
-RRType_NSEC3PARAM(s_RRType *self);
-static PyObject*
-RRType_DNAME(s_RRType *self);
-static PyObject*
-RRType_PTR(s_RRType *self);
-static PyObject*
-RRType_MX(s_RRType *self);
-static PyObject*
-RRType_DNSKEY(s_RRType *self);
-static PyObject*
-RRType_TXT(s_RRType *self);
-static PyObject*
-RRType_RRSIG(s_RRType *self);
-static PyObject*
-RRType_NSEC(s_RRType *self);
-static PyObject*
-RRType_AAAA(s_RRType *self);
-static PyObject*
-RRType_DS(s_RRType *self);
-static PyObject*
-RRType_OPT(s_RRType *self);
-static PyObject*
-RRType_A(s_RRType *self);
-static PyObject*
-RRType_NS(s_RRType *self);
-static PyObject*
-RRType_CNAME(s_RRType *self);
-static PyObject*
-RRType_SOA(s_RRType *self);
-static PyObject*
-RRType_NSEC3(s_RRType *self);
-static PyObject*
-RRType_IXFR(s_RRType *self);
-static PyObject*
-RRType_AXFR(s_RRType *self);
-static PyObject*
-RRType_ANY(s_RRType *self);
+static PyObject* RRType_str(PyObject* self);
+static PyObject* RRType_toWire(s_RRType* self, PyObject* args);
+static PyObject* RRType_getCode(s_RRType* self);
+static PyObject* RRType_richcmp(s_RRType* self, s_RRType* other, int op);
+static PyObject* RRType_NSEC3PARAM(s_RRType *self);
+static PyObject* RRType_DNAME(s_RRType *self);
+static PyObject* RRType_PTR(s_RRType *self);
+static PyObject* RRType_MX(s_RRType *self);
+static PyObject* RRType_DNSKEY(s_RRType *self);
+static PyObject* RRType_TXT(s_RRType *self);
+static PyObject* RRType_RRSIG(s_RRType *self);
+static PyObject* RRType_NSEC(s_RRType *self);
+static PyObject* RRType_AAAA(s_RRType *self);
+static PyObject* RRType_DS(s_RRType *self);
+static PyObject* RRType_OPT(s_RRType *self);
+static PyObject* RRType_A(s_RRType *self);
+static PyObject* RRType_NS(s_RRType *self);
+static PyObject* RRType_CNAME(s_RRType *self);
+static PyObject* RRType_SOA(s_RRType *self);
+static PyObject* RRType_NSEC3(s_RRType *self);
+static PyObject* RRType_IXFR(s_RRType *self);
+static PyObject* RRType_AXFR(s_RRType *self);
+static PyObject* RRType_ANY(s_RRType *self);
 
 // This list contains the actual set of functions we have in
 // python. Each entry has
@@ -356,11 +333,12 @@ RRType_richcmp(s_RRType* self, s_RRType* other, int op) {
 // Common function for RRType_A/NS/etc.
 //
 static PyObject* RRType_createStatic(RRType stc) {
-    s_RRType* ret = PyObject_New(s_RRType, &rrclass_type);
+    s_RRType* ret = PyObject_New(s_RRType, &rrtype_type);
     if (ret != NULL) {
         ret->rrtype = new RRType(stc);
     }
-    return static_cast<PyObject*>(ret);
+    //return static_cast<PyObject*>(ret);
+    return (ret);
 }
 
 static PyObject*

+ 9 - 0
src/lib/dns/python/tests/rdata_python_test.py

@@ -57,5 +57,14 @@ class RdataTest(unittest.TestCase):
         self.assertEqual("\"asdfasdfasdf\"", self.rdata3.to_text())
         self.assertEqual("\"foo\"", self.rdata4.to_text())
 
+    def test_richcmp(self):
+        self.assertTrue(self.rdata1 < self.rdata2);
+        self.assertTrue(self.rdata1 <= self.rdata2);
+        self.assertFalse(self.rdata1 > self.rdata2);
+        self.assertFalse(self.rdata1 >= self.rdata2);
+        self.assertTrue(self.rdata3 != self.rdata4)
+        other_rdata = Rdata(RRType("TXT"), RRClass("IN"), "foo")
+        self.assertTrue(self.rdata4 == other_rdata)
+
 if __name__ == '__main__':
     unittest.main()