From: Bert Vermeulen Date: Mon, 23 Jan 2012 03:51:33 +0000 (+0100) Subject: srd: use new exception system everywhere X-Git-Tag: libsigrokdecode-0.1.0~107 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=commitdiff_plain;h=2086c68463fef416fd9a48dac5e1aa7fa593308d srd: use new exception system everywhere --- diff --git a/controller.c b/controller.c index d205691..d79c607 100644 --- a/controller.c +++ b/controller.c @@ -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); diff --git a/decoder.c b/decoder.c index d29a6d6..f242ffe 100644 --- 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); } diff --git a/type_decoder.c b/type_decoder.c index ab5182f..86ccf02 100644 --- a/type_decoder.c +++ b/type_decoder.c @@ -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 060c253..090c0c1 100644 --- 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;