X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoder.c;h=25ca4767c6961c17536717c473204d6e98ce20b0;hp=5fce5a00026125b8575b0776f26badd6a700ef0e;hb=e9a3c933ea8d77a46b881e525c2cfaf71b23041d;hpb=6d67d057d13318deb0a11de2589b5442df389092 diff --git a/decoder.c b/decoder.c index 5fce5a0..25ca476 100644 --- a/decoder.c +++ b/decoder.c @@ -39,7 +39,7 @@ /** @cond PRIVATE */ -/* The list of protocol decoders. */ +/* The list of loaded protocol decoders. */ static GSList *pd_list = NULL; /* srd.c */ @@ -64,7 +64,7 @@ static gboolean srd_check_init(void) } /** - * Returns the list of supported/loaded protocol decoders. + * Returns the list of loaded protocol decoders. * * This is a GSList of pointers to struct srd_decoder items. * @@ -586,6 +586,29 @@ static int check_method(PyObject *py_dec, const char *mod_name, return SRD_OK; } +/** + * Get the API version of the specified decoder. + * + * @param d The decoder to use. Must not be NULL. + * + * @return The API version of the decoder, or 0 upon errors. + */ +SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d) +{ + PyObject *py_apiver; + long apiver; + + if (!d) + return 0; + + py_apiver = PyObject_GetAttrString(d->py_dec, "api_version"); + apiver = (py_apiver && PyLong_Check(py_apiver)) + ? PyLong_AsLong(py_apiver) : 0; + Py_XDECREF(py_apiver); + + return apiver; +} + /** * Load a protocol decoder module into the embedded Python interpreter. * @@ -597,7 +620,7 @@ static int check_method(PyObject *py_dec, const char *mod_name, */ SRD_API int srd_decoder_load(const char *module_name) { - PyObject *py_modname, *py_basedec, *py_apiver; + PyObject *py_basedec; struct srd_decoder *d; long apiver; int is_subclass; @@ -617,13 +640,7 @@ SRD_API int srd_decoder_load(const char *module_name) d = g_malloc0(sizeof(struct srd_decoder)); - /* Import the Python module. */ - py_modname = PyUnicode_FromString(module_name); - if (!py_modname) - goto except_out; - - d->py_mod = PyImport_Import(py_modname); - Py_DECREF(py_modname); + d->py_mod = py_import_by_name(module_name); if (!d->py_mod) goto except_out; @@ -654,11 +671,7 @@ SRD_API int srd_decoder_load(const char *module_name) * Check that this decoder has the correct PD API version. * PDs of different API versions are incompatible and cannot work. */ - py_apiver = PyObject_GetAttrString(d->py_dec, "api_version"); - apiver = (py_apiver && PyLong_Check(py_apiver)) - ? PyLong_AsLong(py_apiver) : 0; - Py_XDECREF(py_apiver); - + apiver = srd_decoder_apiver(d); if (apiver != 2) { srd_exception_catch("Only PDs of API version 2 are supported"); goto err_out; @@ -710,7 +723,7 @@ SRD_API int srd_decoder_load(const char *module_name) if (get_binary_classes(d) != SRD_OK) goto err_out; - /* Append it to the list of supported/loaded decoders. */ + /* Append it to the list of loaded decoders. */ pd_list = g_slist_append(pd_list, d); return SRD_OK; @@ -793,6 +806,9 @@ SRD_API int srd_decoder_unload(struct srd_decoder *dec) srd_inst_free_all(sess, NULL); } + /* Remove the PD from the list of loaded decoders. */ + pd_list = g_slist_remove(pd_list, dec); + decoder_free(dec); return SRD_OK; @@ -806,15 +822,9 @@ static void srd_decoder_load_all_zip_path(char *path) char *prefix; size_t prefix_len; - zipimport_mod = NULL; set = files = prefix_obj = zipimporter = zipimporter_class = NULL; - modname = PyUnicode_FromString("zipimport"); - if (!modname) - goto err_out; - - zipimport_mod = PyImport_Import(modname); - Py_DECREF(modname); + zipimport_mod = py_import_by_name("zipimport"); if (zipimport_mod == NULL) goto err_out;