X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decode.c;h=0dd1fda71ec45006ac1a6e95d8d62e99ee325dac;hp=e3974b9dbe26a5bb5a9a572510f158410dbcc4f2;hb=f39d2404acb54461f61b676ab164d42e9e76e3fa;hpb=261d65327fa3a70b06d7e5b05c03c44cbb3b7eac diff --git a/decode.c b/decode.c index e3974b9..0dd1fda 100644 --- a/decode.c +++ b/decode.c @@ -60,7 +60,7 @@ static PyObject *emb_put(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "O:put", &arg)) return NULL; - fprintf(stdout, "sigrok.put() called by decoder:\n"); + // fprintf(stdout, "sigrok.put() called by decoder:\n"); PyObject_Print(arg, stdout, Py_PRINT_RAW); puts(""); @@ -106,16 +106,25 @@ int srd_init(void) /* Py_Initialize() returns void and usually cannot fail. */ Py_Initialize(); - Py_InitModule("sigrok", EmbMethods); + /* TODO: Use Py_InitModule3() to add a docstring? */ + if (!Py_InitModule("sigrok", EmbMethods)) { + Py_Finalize(); /* Returns void. */ + return SRD_ERR_PYTHON; + } /* Add search directory for the protocol decoders. */ - /* FIXME: Check error code. */ /* FIXME: What happens if this function is called multiple times? */ - PyRun_SimpleString("import sys;" - "sys.path.append(r'" DECODERS_DIR "');"); + ret = PyRun_SimpleString("import sys;" + "sys.path.append(r'" DECODERS_DIR "');"); + if (ret != 0) { + Py_Finalize(); /* Returns void. */ + return SRD_ERR_PYTHON; + } - if (!(dir = opendir(DECODERS_DIR))) + if (!(dir = opendir(DECODERS_DIR))) { + Py_Finalize(); /* Returns void. */ return SRD_ERR_DECODERS_DIR; + } while ((dp = readdir(dir)) != NULL) { /* Ignore filenames which don't end with ".py". */ @@ -126,9 +135,13 @@ int srd_init(void) decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); /* TODO: Error handling. Use g_try_malloc(). */ - dec = malloc(sizeof(struct srd_decoder)); + if (!(dec = malloc(sizeof(struct srd_decoder)))) { + Py_Finalize(); /* Returns void. */ + return SRD_ERR_MALLOC; + } /* Load the decoder. */ + /* TODO: Warning if loading fails for a decoder. */ ret = srd_load_decoder(decodername, &dec); if (!ret) { /* Append it to the list of supported/loaded decoders. */ @@ -450,6 +463,8 @@ static int srd_unload_decoder(struct srd_decoder *dec) Py_XDECREF(dec->py_decobj); Py_XDECREF(dec->py_mod); + /* TODO: (g_)free dec itself? */ + return SRD_OK; }