2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "libsigrokdecode.h"
26 static char *py_stringify(PyObject *py_obj)
28 PyObject *py_str, *py_bytes;
34 py_str = PyObject_Str(py_obj);
35 if (!py_str || !PyUnicode_Check(py_str))
38 py_bytes = PyUnicode_AsUTF8String(py_str);
42 str = g_strdup(PyBytes_AsString(py_bytes));
49 srd_dbg("Failed to stringify object.");
54 static char *py_get_string_attr(PyObject *py_obj, const char *attr)
56 PyObject *py_str, *py_bytes;
62 py_str = PyObject_GetAttrString(py_obj, attr);
63 if (!py_str || !PyUnicode_Check(py_str))
66 py_bytes = PyUnicode_AsUTF8String(py_str);
70 str = g_strdup(PyBytes_AsString(py_bytes));
77 srd_dbg("Failed to get object attribute %s.", attr);
83 SRD_PRIV void srd_exception_catch(const char *format, ...)
86 PyObject *py_etype, *py_evalue, *py_etraceback;
87 PyObject *py_mod, *py_func, *py_tracefmt;
88 char *msg, *etype_name, *evalue_str, *tracefmt_str;
89 const char *etype_name_fallback;
91 py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL;
93 va_start(args, format);
94 msg = g_strdup_vprintf(format, args);
97 PyErr_Fetch(&py_etype, &py_evalue, &py_etraceback);
99 /* No current exception, so just print the message. */
103 PyErr_NormalizeException(&py_etype, &py_evalue, &py_etraceback);
105 etype_name = py_get_string_attr(py_etype, "__name__");
106 evalue_str = py_stringify(py_evalue);
107 etype_name_fallback = (etype_name) ? etype_name : "(unknown exception)";
110 srd_err("%s: %s: %s", etype_name_fallback, msg, evalue_str);
112 srd_err("%s: %s.", etype_name_fallback, msg);
117 /* If there is no traceback object, we are done. */
121 py_mod = py_import_by_name("traceback");
125 py_func = PyObject_GetAttrString(py_mod, "format_exception");
126 if (!py_func || !PyCallable_Check(py_func))
129 /* Call into Python to format the stack trace. */
130 py_tracefmt = PyObject_CallFunctionObjArgs(py_func,
131 py_etype, py_evalue, py_etraceback, NULL);
135 tracefmt_str = py_stringify(py_tracefmt);
136 Py_DECREF(py_tracefmt);
138 /* Log the detailed stack trace. */
140 srd_dbg("%s", tracefmt_str);
141 g_free(tracefmt_str);
147 Py_XDECREF(py_etraceback);
148 Py_XDECREF(py_evalue);
149 Py_XDECREF(py_etype);