]> sigrok.org Git - libsigrokdecode.git/commitdiff
srd: decode.c: Improve error handling a bit.
authorUwe Hermann <redacted>
Wed, 23 Nov 2011 08:17:48 +0000 (09:17 +0100)
committerUwe Hermann <redacted>
Wed, 23 Nov 2011 08:17:48 +0000 (09:17 +0100)
decode.c

index e3974b9dbe26a5bb5a9a572510f158410dbcc4f2..727134a4c45bfbd939429c52781866579cf04697 100644 (file)
--- a/decode.c
+++ b/decode.c
@@ -106,16 +106,25 @@ int srd_init(void)
        /* Py_Initialize() returns void and usually cannot fail. */
        Py_Initialize();
 
        /* 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. */
 
        /* Add search directory for the protocol decoders. */
-       /* FIXME: Check error code. */
        /* FIXME: What happens if this function is called multiple times? */
        /* 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;
                return SRD_ERR_DECODERS_DIR;
+       }
 
        while ((dp = readdir(dir)) != NULL) {
                /* Ignore filenames which don't end with ".py". */
 
        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(). */
                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. */
 
                /* 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. */
                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);
 
        Py_XDECREF(dec->py_decobj);
        Py_XDECREF(dec->py_mod);
 
+       /* TODO: (g_)free dec itself? */
+
        return SRD_OK;
 }
 
        return SRD_OK;
 }