From: Uwe Hermann Date: Thu, 25 Aug 2016 20:22:07 +0000 (+0200) Subject: Factor out srd_decoder_apiver(). X-Git-Tag: libsigrokdecode-0.4.1~11 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=6ebb97f7aa2499db801610369df61c59b01c5611;p=libsigrokdecode.git Factor out srd_decoder_apiver(). --- diff --git a/decoder.c b/decoder.c index 748280a..25ca476 100644 --- a/decoder.c +++ b/decoder.c @@ -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_basedec, *py_apiver; + PyObject *py_basedec; struct srd_decoder *d; long apiver; int is_subclass; @@ -648,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; diff --git a/libsigrokdecode-internal.h b/libsigrokdecode-internal.h index d32414a..8259b97 100644 --- a/libsigrokdecode-internal.h +++ b/libsigrokdecode-internal.h @@ -86,6 +86,9 @@ SRD_PRIV int srd_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3); #define srd_warn(...) srd_log(SRD_LOG_WARN, __VA_ARGS__) #define srd_err(...) srd_log(SRD_LOG_ERR, __VA_ARGS__) +/* decoder.c */ +SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d); + /* type_decoder.c */ SRD_PRIV PyObject *srd_Decoder_type_new(void);