Browse Source

check types in all richcmp functions

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

+ 1 - 16
src/lib/dns/python/TODO

@@ -4,27 +4,12 @@ add statics for RRClass::IN() (RRClass.IN()) etc.
 
 same for RRType? (xfrout.py.in line 256)
 
-__str__ for name, question, everything with to_text()
-rich compare for name (at least so we can have ==)
-
-should Name.downcase() return a ref to itself?
-
-All constructors based on buffers need an optional position
-argument (like question_python has now)
-
-at question.to_wire(bytes) does not seem to work right (only return
-value seems correct, while i'd like in-place addition if possible)
-
 creating a render message and not setting opcode/rcode results in a segfault later (nullpointer)
-
+(is this cpp or python problem?)
 
 The function set wrapped is not complete; for instance, in
 MessageRenderer, we really only provide the high-level readout
 functions. Do we need access to the writers? (there is one set() right
 now).
 
-All constants are now added named in the base module, while they should
-be added as class constants. Dunno how though.
-
-
 segfault when comparing with bad type like int (at least for Name and Rcode, but probably for the rest too)

+ 24 - 0
src/lib/dns/python/message_python.cc

@@ -624,6 +624,14 @@ Opcode_richcmp(s_Opcode* self, s_Opcode* other, int op)
 {
     bool c = false;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other ||
+        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     // Only equals and not equals here, unorderable type
     switch (op) {
     case Py_LT:
@@ -1089,6 +1097,14 @@ Rcode_richcmp(s_Rcode* self, s_Rcode* other, int op)
 {
     bool c;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other ||
+        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     // Only equals and not equals here, unorderable type
     switch (op) {
     case Py_LT:
@@ -1296,6 +1312,14 @@ Section_richcmp(s_Section* self, s_Section* other, int op)
 {
     bool c;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other ||
+        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     // Only equals and not equals here, unorderable type
     switch (op) {
     case Py_LT:

+ 11 - 2
src/lib/dns/python/name_python.cc

@@ -218,7 +218,7 @@ static PyMethodDef Name_methods[] = {
       "Concatenates the given Name object to this one and returns the "
       "result as a new Name object" },
     { "downcase", (PyCFunction)Name_downcase, METH_NOARGS,
-      "Downcases this name object (in-place)." },
+      "Downcases this name object (in-place). Returns a new reference to the Name." },
     { "is_wildcard", (PyCFunction)Name_isWildCard, METH_NOARGS,
       "Returns True if the Name object represents a wildcard name." },
     { NULL, NULL, 0, NULL }
@@ -500,6 +500,14 @@ Name_richcmp(s_Name* n1, s_Name* n2, int op)
 {
     bool c;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!n2 ||
+        ((PyObject*)n1)->ob_type != ((PyObject*)n2)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     switch (op) {
     case Py_LT:
         c = n1->name->lthan(*n2->name);
@@ -572,7 +580,8 @@ static PyObject*
 Name_downcase(s_Name* self)
 {
     self->name->downcase();
-    Py_RETURN_NONE;
+    Py_INCREF(self);
+    return (PyObject*) self;
 }
 
 static PyObject*

+ 8 - 0
src/lib/dns/python/rrclass_python.cc

@@ -252,6 +252,14 @@ RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op)
 {
     bool c;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other ||
+        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     switch (op) {
     case Py_LT:
         c = *self->rrclass < *other->rrclass;

+ 8 - 0
src/lib/dns/python/rrttl_python.cc

@@ -257,6 +257,14 @@ RRTTL_richcmp(s_RRTTL* self, s_RRTTL* other, int op)
 {
     bool c;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other ||
+        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     switch (op) {
     case Py_LT:
         c = *self->rrttl < *other->rrttl;

+ 8 - 0
src/lib/dns/python/rrtype_python.cc

@@ -260,6 +260,14 @@ RRType_richcmp(s_RRType* self, s_RRType* other, int op)
 {
     bool c;
 
+    // Check for null and if the types match. If different type,
+    // simply return False
+    if (!other ||
+        ((PyObject*)self)->ob_type != ((PyObject*)other)->ob_type
+       ) {
+        Py_RETURN_FALSE;
+    }
+
     switch (op) {
     case Py_LT:
         c = *self->rrtype < *other->rrtype;