Browse Source

[1245] catch exceptions where createXXXObject() is called

and removed unused createMessageRendererObject function
Jelte Jansen 13 years ago
parent
commit
2f8c4b3da6

+ 52 - 17
src/lib/dns/python/message_python.cc

@@ -311,8 +311,14 @@ Message_getOpcode(s_Message* self) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Failed to get message opcode: " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+        return (NULL);
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception getting opcode from message");
         return (NULL);
     }
 }
@@ -338,7 +344,17 @@ Message_getEDNS(s_Message* self) {
     if (!src) {
         Py_RETURN_NONE;
     }
-    return (createEDNSObject(*src));
+    try {
+        return (createEDNSObject(*src));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Failed to get EDNS from message: " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting EDNS from message");
+    }
+    return (NULL);
 }
 
 PyObject*
@@ -418,13 +434,25 @@ Message_getQuestion(s_Message* self) {
         return (NULL);
     }
 
-    for (; qi != qi_end; ++qi) {
-        if (PyList_Append(list, createQuestionObject(**qi)) == -1) {
-            Py_DECREF(list);
-            return (NULL);
+    try {
+        for (; qi != qi_end; ++qi) {
+            if (PyList_Append(list, createQuestionObject(**qi)) == -1) {
+                Py_DECREF(list);
+                return (NULL);
+            }
         }
+        return (list);
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting Question section: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting Question section");
     }
-    return (list);
+    Py_DECREF(list);
+    return (NULL);
 }
 
 PyObject*
@@ -458,18 +486,25 @@ Message_getSection(s_Message* self, PyObject* args) {
     if (list == NULL) {
         return (NULL);
     }
-    for (; rrsi != rrsi_end; ++rrsi) {
-        PyObject* rrset = createRRsetObject(**rrsi);
-        if (PyList_Append(list, rrset) == -1) {
-                Py_DECREF(rrset);
-                Py_DECREF(list);
-                return (NULL);
+    try {
+        for (; rrsi != rrsi_end; ++rrsi) {
+            if (PyList_Append(list, createRRsetObject(**rrsi)) == -1) {
+                    Py_DECREF(list);
+                    return (NULL);
+            }
         }
-        // PyList_Append increases refcount, so we remove ours since
-        // we don't need it anymore
-        Py_DECREF(rrset);
+        return (list);
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure creating Question object: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure creating Question object");
     }
-    return (list);
+    Py_DECREF(list);
+    return (NULL);
 }
 
 //static PyObject* Message_beginQuestion(s_Message* self, PyObject* args);

+ 3 - 22
src/lib/dns/python/messagerenderer_python.cc

@@ -239,28 +239,9 @@ PyTypeObject messagerenderer_type = {
     0                                   // tp_version_tag
 };
 
-PyObject*
-createMessageRendererObject(const MessageRenderer& source) {
-    // should we copy? can we?
-    // copy the existing buffer into a new one, then create a new renderer with
-    // that buffer
-    s_MessageRenderer* mr = static_cast<s_MessageRenderer*>(
-        messagerenderer_type.tp_alloc(&messagerenderer_type, 0));
-    if (mr == NULL) {
-        isc_throw(PyCPPWrapperException, "Unexpected NULL C++ object, "
-                  "probably due to short memory");
-    }
-    try {
-        mr->outputbuffer = new OutputBuffer(4096);
-        mr->outputbuffer->writeData(source.getData(), source.getLength());
-        mr->cppobj = new MessageRenderer(*mr->outputbuffer);
-
-        return (mr);
-    } catch (const std::bad_alloc&) {
-        isc_throw(PyCPPWrapperException, "Unexpected NULL C++ object, "
-                  "probably due to short memory");
-    }
-}
+// If we need a createMessageRendererObject(), should we copy? can we?
+// copy the existing buffer into a new one, then create a new renderer with
+// that buffer?
 
 bool
 PyMessageRenderer_Check(PyObject* obj) {

+ 0 - 9
src/lib/dns/python/messagerenderer_python.h

@@ -26,15 +26,6 @@ namespace python {
 
 extern PyTypeObject messagerenderer_type;
 
-/// This is a simple shortcut to create a python MessageRenderer object (in the
-/// form of a pointer to PyObject) with minimal exception safety.
-/// On success, it returns a valid pointer to PyObject with a reference
-/// counter of 1; if something goes wrong it throws an exception (it never
-/// returns a NULL pointer).
-/// This function is expected to be called within a try block
-/// followed by necessary setup for python exception.
-PyObject* createMessageRendererObject(const MessageRenderer& source);
-
 /// \brief Checks if the given python object is a MessageRenderer object
 ///
 /// \exception PyCPPWrapperException if obj is NULL

+ 38 - 4
src/lib/dns/python/question_python.cc

@@ -20,13 +20,14 @@
 #include <util/buffer.h>
 #include <util/python/pycppwrapper_util.h>
 
+#include "pydnspp_common.h"
 #include "question_python.h"
-
 #include "name_python.h"
 #include "rrclass_python.h"
 #include "rrtype_python.h"
 #include "messagerenderer_python.h"
 
+using namespace std;
 using namespace isc::dns;
 using namespace isc::dns::python;
 using namespace isc::util;
@@ -139,17 +140,50 @@ Question_destroy(s_Question* self) {
 
 static PyObject*
 Question_getName(s_Question* self) {
-    return (createNameObject(self->cppobj->getName()));
+    try {
+        return (createNameObject(self->cppobj->getName()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting question Name: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting question Name");
+    }
+    return (NULL);
 }
 
 static PyObject*
 Question_getType(s_Question* self) {
-    return (createRRTypeObject(self->cppobj->getType()));
+    try {
+        return (createRRTypeObject(self->cppobj->getType()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting question RRType: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting question RRType");
+    }
+    return (NULL);
 }
 
 static PyObject*
 Question_getClass(s_Question* self) {
-    return (createRRClassObject(self->cppobj->getClass()));
+    try {
+        return (createRRClassObject(self->cppobj->getClass()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting question RRClass: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting question RRClass");
+    }
+    return (NULL);
 }
 
 static PyObject*

+ 70 - 11
src/lib/dns/python/rrset_python.cc

@@ -29,6 +29,7 @@
 #include "rdata_python.h"
 #include "messagerenderer_python.h"
 
+using namespace std;
 using namespace isc::dns;
 using namespace isc::dns::python;
 using namespace isc::util;
@@ -138,22 +139,66 @@ RRset_getRdataCount(s_RRset* self) {
 
 PyObject*
 RRset_getName(s_RRset* self) {
-    return (createNameObject(self->cppobj->getName()));
+    try {
+        return (createNameObject(self->cppobj->getName()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting rrset Name: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting rrset Name");
+    }
+    return (NULL);
 }
 
 PyObject*
 RRset_getClass(s_RRset* self) {
-    return (createRRClassObject(self->cppobj->getClass()));
+    try {
+        return (createRRClassObject(self->cppobj->getClass()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting question RRClass: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting question RRClass");
+    }
+    return (NULL);
 }
 
 PyObject*
 RRset_getType(s_RRset* self) {
-    return (createRRTypeObject(self->cppobj->getType()));
+    try {
+        return (createRRTypeObject(self->cppobj->getType()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting question RRType: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting question RRType");
+    }
+    return (NULL);
 }
 
 PyObject*
 RRset_getTTL(s_RRset* self) {
-    return (createRRTTLObject(self->cppobj->getTTL()));
+    try {
+        return (createRRTTLObject(self->cppobj->getTTL()));
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting question TTL: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting question TTL");
+    }
+    return (NULL);
 }
 
 PyObject*
@@ -251,14 +296,28 @@ RRset_getRdata(s_RRset* self) {
 
     RdataIteratorPtr it = self->cppobj->getRdataIterator();
 
-    for (; !it->isLast(); it->next()) {
-        const rdata::Rdata *rd = &it->getCurrent();
-        PyList_Append(list,
-                      createRdataObject(createRdata(self->cppobj->getType(),
-                                        self->cppobj->getClass(), *rd)));
+    try {
+        for (; !it->isLast(); it->next()) {
+            const rdata::Rdata *rd = &it->getCurrent();
+            if (PyList_Append(list,
+                    createRdataObject(createRdata(self->cppobj->getType(),
+                                      self->cppobj->getClass(), *rd))) == -1) {
+                Py_DECREF(list);
+                return (NULL);
+            }
+        }
+        return (list);
+    } catch (const exception& ex) {
+        const string ex_what =
+            "Unexpected failure getting rrset Rdata: " +
+            string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
+    } catch (...) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Unexpected failure getting rrset Rdata");
     }
-
-    return (list);
+    Py_DECREF(list);
+    return (NULL);
 }
 
 PyObject*

+ 1 - 1
src/lib/dns/python/tsig_python.cc

@@ -148,7 +148,7 @@ PyObject*
 TSIGContext_getError(s_TSIGContext* self) {
     try {
         PyObjectContainer container(createTSIGErrorObject(
-                                        self->cppobj->getError()));
+                                    self->cppobj->getError()));
         return (Py_BuildValue("O", container.get()));
     } catch (const exception& ex) {
         const string ex_what =