From: Bert Vermeulen Date: Wed, 28 Dec 2011 12:40:23 +0000 (+0100) Subject: better python memory management X-Git-Tag: libsigrokdecode-0.1.0~233 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8b4bbd2a0bcfdc2e186b345de087f23b9c904080;p=libsigrokdecode.git better python memory management --- diff --git a/decoder.c b/decoder.c index 5d719d3..9f951c1 100644 --- a/decoder.c +++ b/decoder.c @@ -96,30 +96,30 @@ int srd_load_decoder(const char *name, struct srd_decoder **dec) if (!(d = malloc(sizeof(struct srd_decoder)))) return SRD_ERR_MALLOC; - if ((r = h_str(py_res, py_mod, "id", &(d->id))) < 0) + if ((r = h_str(py_res, "id", &(d->id))) < 0) return r; - if ((r = h_str(py_res, py_mod, "name", &(d->name))) < 0) + if ((r = h_str(py_res, "name", &(d->name))) < 0) return r; - if ((r = h_str(py_res, py_mod, "longname", + if ((r = h_str(py_res, "longname", &(d->longname))) < 0) return r; - if ((r = h_str(py_res, py_mod, "desc", &(d->desc))) < 0) + if ((r = h_str(py_res, "desc", &(d->desc))) < 0) return r; - if ((r = h_str(py_res, py_mod, "longdesc", + if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0) return r; - if ((r = h_str(py_res, py_mod, "author", &(d->author))) < 0) + if ((r = h_str(py_res, "author", &(d->author))) < 0) return r; - if ((r = h_str(py_res, py_mod, "email", &(d->email))) < 0) + if ((r = h_str(py_res, "email", &(d->email))) < 0) return r; - if ((r = h_str(py_res, py_mod, "license", &(d->license))) < 0) + if ((r = h_str(py_res, "license", &(d->license))) < 0) return r; d->py_mod = py_mod; diff --git a/sigrokdecode.h b/sigrokdecode.h index 6005ad9..8266fb5 100644 --- a/sigrokdecode.h +++ b/sigrokdecode.h @@ -151,8 +151,7 @@ int srd_load_all_decoders(void); int srd_unload_all_decoders(void); /*--- util.c ----------------------------------------------------------------*/ - -int h_str(PyObject *py_res, PyObject *py_mod, const char *key, char **outstr); +int h_str(PyObject *py_res, const char *key, char **outstr); /*--- log.c -----------------------------------------------------------------*/ diff --git a/util.c b/util.c index b6de2fb..0885626 100644 --- a/util.c +++ b/util.c @@ -23,54 +23,57 @@ /** - * Helper function to handle Python strings. + * Helper function to get the value of a python object's attribute, + * returned as a newly allocated char *. * - * TODO: @param entries. + * @param py_obj The object to probe. + * @param key Name of the attribute to retrieve. + * @param outstr ptr to char * storage to be filled in. * * @return SRD_OK upon success, a (negative) error code otherwise. * The 'outstr' argument points to a malloc()ed string upon success. */ -int h_str(PyObject *py_res, PyObject *py_mod, const char *key, char **outstr) +int h_str(PyObject *py_obj, const char *key, char **outstr) { PyObject *py_str, *py_encstr; char *str; int ret; - if (!(py_str = PyObject_GetAttrString(py_res, (char *)key))) { - ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ - goto err_h_decref_mod; + py_str = py_encstr = NULL; + str = NULL; + ret = SRD_OK; + + if (!(py_str = PyObject_GetAttrString(py_obj, (char *)key))) { + /* TODO: log level 4 debug message */ + ret = SRD_ERR_PYTHON; + goto err_out; } - /* - * PyBytes_AsString()'s returned string refers to an internal buffer - * (not a copy), i.e. the data must not be modified, and the memory - * must not be free()'d. - */ if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) { - ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ - goto err_h_decref_str; + /* TODO: log level 4 debug message */ + ret = SRD_ERR_PYTHON; + goto err_out; } if (!(str = PyBytes_AS_STRING(py_encstr))) { - ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ - goto err_h_decref_str; + /* TODO: log level 4 debug message */ + ret = SRD_ERR_PYTHON; + goto err_out; } if (!(*outstr = g_strdup(str))) { + /* TODO: log level 4 debug message */ ret = SRD_ERR_MALLOC; - goto err_h_decref_str; + goto err_out; } - Py_XDECREF(py_str); - - return SRD_OK; - -err_h_decref_str: - Py_XDECREF(py_str); -err_h_decref_mod: - Py_XDECREF(py_mod); +err_out: + if (py_str) + Py_XDECREF(py_str); + if (py_encstr) + Py_XDECREF(py_encstr); if (PyErr_Occurred()) - PyErr_Print(); /* Returns void. */ + PyErr_Print(); return ret; }