]> sigrok.org Git - libsigrokdecode.git/commitdiff
srd_exception_catch(): Beautify traceback logging output.
authorUwe Hermann <redacted>
Fri, 20 Apr 2018 21:30:58 +0000 (23:30 +0200)
committerUwe Hermann <redacted>
Fri, 20 Apr 2018 21:30:58 +0000 (23:30 +0200)
exception.c

index 8058fbfa44f02bbe3a8292d8cb6c7efc444f13ab..8b508837cffa39a2acf14c8d443daaef92dea6fc 100644 (file)
@@ -86,12 +86,14 @@ cleanup:
 /** @private */
 SRD_PRIV void srd_exception_catch(const char *format, ...)
 {
+       int i, ret;
        va_list args;
        PyObject *py_etype, *py_evalue, *py_etraceback;
        PyObject *py_mod, *py_func, *py_tracefmt;
-       char *msg, *etype_name, *evalue_str, *tracefmt_str;
+       char *msg, *etype_name, *evalue_str, *outstr;
        const char *etype_name_fallback;
        PyGILState_STATE gstate;
+       GString *s;
 
        py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL;
 
@@ -136,17 +138,21 @@ SRD_PRIV void srd_exception_catch(const char *format, ...)
        /* Call into Python to format the stack trace. */
        py_tracefmt = PyObject_CallFunctionObjArgs(py_func,
                        py_etype, py_evalue, py_etraceback, NULL);
-       if (!py_tracefmt)
+       if (!py_tracefmt || !PyList_Check(py_tracefmt))
                goto cleanup;
 
-       tracefmt_str = py_stringify(py_tracefmt);
-       Py_DECREF(py_tracefmt);
-
-       /* Log the detailed stack trace. */
-       if (tracefmt_str) {
-               srd_err("%s", tracefmt_str);
-               g_free(tracefmt_str);
+       s = g_string_sized_new(128);
+       for (i = 0; i < PyList_Size(py_tracefmt); i++) {
+               ret = py_listitem_as_str(py_tracefmt, i, &outstr);
+               if (ret == 0) {
+                       s = g_string_append(s, outstr);
+                       g_free(outstr);
+               }
        }
+       srd_err("%s", s->str);
+       g_string_free(s, TRUE);
+
+       Py_DECREF(py_tracefmt);
 
 cleanup:
        Py_XDECREF(py_func);