srd: use new exception system everywhere
authorBert Vermeulen <bert@biot.com>
Mon, 23 Jan 2012 03:51:33 +0000 (04:51 +0100)
committerBert Vermeulen <bert@biot.com>
Mon, 23 Jan 2012 03:53:32 +0000 (04:53 +0100)
controller.c
decoder.c
type_decoder.c
util.c

index d2056915b831c961c90cc1373ecf395c89e7f478..d79c607d550f0c3356ee23147c74cfbf56c77746 100644 (file)
@@ -266,11 +266,8 @@ err_out:
        Py_XDECREF(py_dec_options);
        if (key)
                g_free(key);
-       if (PyErr_Occurred()) {
-               srd_dbg("srd: stray exception!");
-               PyErr_Print();
-               PyErr_Clear();
-       }
+       if (PyErr_Occurred())
+               catch_exception("srd: stray exception in srd_instance_set_options()");
 
        return ret;
 }
@@ -394,7 +391,7 @@ struct srd_decoder_instance *srd_instance_new(const char *decoder_id,
        /* Create a new instance of this decoder class. */
        if (!(di->py_instance = PyObject_CallObject(dec->py_dec, NULL))) {
                if (PyErr_Occurred())
-                       PyErr_Print();
+                       catch_exception("failed to create %s instance: ", decoder_id);
                g_free(di->dec_probemap);
                g_free(di);
                return NULL;
@@ -463,15 +460,13 @@ int srd_instance_start(struct srd_decoder_instance *di, PyObject *args)
 
        if (!(py_name = PyUnicode_FromString("start"))) {
                srd_err("Unable to build python object for 'start'.");
-               if (PyErr_Occurred())
-                       PyErr_Print();
+               catch_exception("Protocol decoder instance %s: ", di->instance_id);
                return SRD_ERR_PYTHON;
        }
 
        if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance,
                        py_name, args, NULL))) {
-               if (PyErr_Occurred())
-                       PyErr_Print();
+               catch_exception("Protocol decoder instance %s: ", di->instance_id);
                return SRD_ERR_PYTHON;
        }
 
@@ -533,9 +528,7 @@ int srd_instance_decode(uint64_t start_samplenum,
        end_samplenum = start_samplenum + inbuflen / di->data_unitsize;
        if (!(py_res = PyObject_CallMethod(di->py_instance, "decode",
                        "KKO", logic->start_samplenum, end_samplenum, logic))) {
-               if (PyErr_Occurred())
-                       PyErr_Print(); /* Returns void. */
-
+               catch_exception("Protocol decoder instance %s: ", di->instance_id);
                return SRD_ERR_PYTHON; /* TODO: More specific error? */
        }
        Py_DecRef(py_res);
index d29a6d6a9472208673ca0ec27ed03428638d8239..f242ffedc770174a2a534576f3fd0436ab8a0d51 100644 (file)
--- a/decoder.c
+++ b/decoder.c
@@ -151,17 +151,13 @@ int srd_load_decoder(const char *name, struct srd_decoder **dec)
 
        /* Import the Python module. */
        if (!(d->py_mod = PyImport_ImportModule(name))) {
-               /* TODO: Report exception message/traceback to err/dbg. */
-               srd_warn("srd: import of '%s' failed.", name);
-               PyErr_Print();
-               PyErr_Clear();
+               catch_exception("import of '%s' failed.", name);
                goto err_out;
        }
 
        /* Get the 'Decoder' class as Python object. */
        if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
                /* This generated an AttributeError exception. */
-               PyErr_Print();
                PyErr_Clear();
                srd_err("Decoder class not found in protocol decoder %s.", name);
                goto err_out;
@@ -294,7 +290,7 @@ char *srd_decoder_doc(struct srd_decoder *dec)
                return NULL;
 
        if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
-               PyErr_Clear();
+               catch_exception("");
                return NULL;
        }
 
@@ -354,14 +350,9 @@ int srd_load_all_decoders(void)
                /* The decoder name is the PD directory name (e.g. "i2c"). */
                decodername = g_strdup(direntry);
 
-               /* TODO: Error handling. Use g_try_malloc(). */
-               if (!(dec = malloc(sizeof(struct srd_decoder)))) {
-                       Py_Finalize(); /* Returns void. */
+               if (!(dec = g_try_malloc(sizeof(struct srd_decoder))))
                        return SRD_ERR_MALLOC;
-               }
 
-               /* Load the decoder. */
-               /* TODO: Warning if loading fails for a decoder. */
                if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
                        /* Append it to the list of supported/loaded decoders. */
                        pd_list = g_slist_append(pd_list, dec);
@@ -382,7 +373,6 @@ int srd_unload_all_decoders(void)
 
        for (l = srd_list_decoders(); l; l = l->next) {
                dec = l->data;
-               /* TODO: Error handling. */
                srd_unload_decoder(dec);
        }
 
index ab5182f0112a3cc522f594b0846847fa4bfe06ad..86ccf02ea7fcd46a3d7626b7f8992e2695962adf 100644 (file)
@@ -126,8 +126,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        Py_XINCREF(next_di->py_instance);
                        if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
                                        "KKO", start_sample, end_sample, data))) {
-                               if (PyErr_Occurred())
-                                       PyErr_Print();
+                               catch_exception("calling %s decode(): ", next_di->instance_id);
                        }
                        Py_XDECREF(py_res);
                }
@@ -155,14 +154,13 @@ static PyObject *Decoder_add(PyObject *self, PyObject *args)
        int output_type, pdo_id;
 
        if (!(di = get_di_by_decobject(self))) {
-               srd_dbg("srd: %s():%d decoder instance not found", __func__, __LINE__);
+               srd_dbg("srd: decoder instance not found");
                PyErr_SetString(PyExc_Exception, "decoder instance not found");
                return NULL;
        }
 
        if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
-               if (PyErr_Occurred())
-                       PyErr_Print();
+               catch_exception("");
                return NULL;
        }
 
diff --git a/util.c b/util.c
index 060c25387df580d3df6f32597132eacbbaaf5f77..090c0c156d267ecf45d10094fa6347b6de061a2c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -46,8 +46,7 @@ int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
        }
 
        if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
-               /* TODO: report exception message/traceback to err/dbg */
-               PyErr_Clear();
+               catch_exception("");
                return SRD_ERR_PYTHON;
        }
 
@@ -149,9 +148,7 @@ err_out:
                Py_XDECREF(py_encstr);
 
        if (PyErr_Occurred()) {
-               srd_dbg("srd: string conversion failed");
-               /* TODO: dump exception to srd_dbg */
-               PyErr_Clear();
+               catch_exception("string conversion failed");
        }
 
        return ret;