]>
Commit | Line | Data |
---|---|---|
ec871a27 | 1 | /* |
50bd5d25 | 2 | * This file is part of the libsigrokdecode project. |
ec871a27 BV |
3 | * |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
36784362 | 20 | #include <config.h> |
f6c7eade MC |
21 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
22 | #include "libsigrokdecode.h" | |
ec871a27 BV |
23 | #include <stdarg.h> |
24 | #include <glib.h> | |
201a85a8 DE |
25 | |
26 | static char *py_stringify(PyObject *py_obj) | |
27 | { | |
28 | PyObject *py_str, *py_bytes; | |
29 | char *str = NULL; | |
30 | ||
514b2edc UH |
31 | /* Note: Caller already ran PyGILState_Ensure(). */ |
32 | ||
201a85a8 DE |
33 | if (!py_obj) |
34 | return NULL; | |
35 | ||
36 | py_str = PyObject_Str(py_obj); | |
37 | if (!py_str || !PyUnicode_Check(py_str)) | |
38 | goto cleanup; | |
39 | ||
40 | py_bytes = PyUnicode_AsUTF8String(py_str); | |
41 | if (!py_bytes) | |
42 | goto cleanup; | |
43 | ||
44 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
45 | Py_DECREF(py_bytes); | |
46 | ||
47 | cleanup: | |
48 | Py_XDECREF(py_str); | |
49 | if (!str) { | |
50 | PyErr_Clear(); | |
51 | srd_dbg("Failed to stringify object."); | |
52 | } | |
53 | return str; | |
54 | } | |
55 | ||
56 | static char *py_get_string_attr(PyObject *py_obj, const char *attr) | |
57 | { | |
58 | PyObject *py_str, *py_bytes; | |
59 | char *str = NULL; | |
60 | ||
514b2edc UH |
61 | /* Note: Caller already ran PyGILState_Ensure(). */ |
62 | ||
201a85a8 DE |
63 | if (!py_obj) |
64 | return NULL; | |
65 | ||
66 | py_str = PyObject_GetAttrString(py_obj, attr); | |
67 | if (!py_str || !PyUnicode_Check(py_str)) | |
68 | goto cleanup; | |
69 | ||
70 | py_bytes = PyUnicode_AsUTF8String(py_str); | |
71 | if (!py_bytes) | |
72 | goto cleanup; | |
73 | ||
74 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
75 | Py_DECREF(py_bytes); | |
76 | ||
77 | cleanup: | |
78 | Py_XDECREF(py_str); | |
79 | if (!str) { | |
80 | PyErr_Clear(); | |
81 | srd_dbg("Failed to get object attribute %s.", attr); | |
82 | } | |
83 | return str; | |
84 | } | |
ec871a27 | 85 | |
57790bc8 | 86 | /** @private */ |
aafeeaea | 87 | SRD_PRIV void srd_exception_catch(const char *format, ...) |
ec871a27 | 88 | { |
ec871a27 | 89 | va_list args; |
201a85a8 | 90 | PyObject *py_etype, *py_evalue, *py_etraceback; |
e9dd2fea | 91 | PyObject *py_mod, *py_func, *py_tracefmt; |
201a85a8 DE |
92 | char *msg, *etype_name, *evalue_str, *tracefmt_str; |
93 | const char *etype_name_fallback; | |
514b2edc | 94 | PyGILState_STATE gstate; |
ec871a27 | 95 | |
201a85a8 | 96 | py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL; |
ec871a27 | 97 | |
201a85a8 DE |
98 | va_start(args, format); |
99 | msg = g_strdup_vprintf(format, args); | |
100 | va_end(args); | |
ec871a27 | 101 | |
514b2edc UH |
102 | gstate = PyGILState_Ensure(); |
103 | ||
201a85a8 DE |
104 | PyErr_Fetch(&py_etype, &py_evalue, &py_etraceback); |
105 | if (!py_etype) { | |
106 | /* No current exception, so just print the message. */ | |
107 | srd_err("%s.", msg); | |
108 | goto cleanup; | |
ec871a27 | 109 | } |
201a85a8 | 110 | PyErr_NormalizeException(&py_etype, &py_evalue, &py_etraceback); |
ec871a27 | 111 | |
201a85a8 DE |
112 | etype_name = py_get_string_attr(py_etype, "__name__"); |
113 | evalue_str = py_stringify(py_evalue); | |
114 | etype_name_fallback = (etype_name) ? etype_name : "(unknown exception)"; | |
115 | ||
116 | if (evalue_str) | |
117 | srd_err("%s: %s: %s", etype_name_fallback, msg, evalue_str); | |
ec871a27 | 118 | else |
201a85a8 | 119 | srd_err("%s: %s.", etype_name_fallback, msg); |
ec871a27 | 120 | |
201a85a8 DE |
121 | g_free(evalue_str); |
122 | g_free(etype_name); | |
123 | ||
124 | /* If there is no traceback object, we are done. */ | |
125 | if (!py_etraceback) | |
126 | goto cleanup; | |
127 | ||
e9dd2fea | 128 | py_mod = py_import_by_name("traceback"); |
201a85a8 DE |
129 | if (!py_mod) |
130 | goto cleanup; | |
131 | ||
132 | py_func = PyObject_GetAttrString(py_mod, "format_exception"); | |
133 | if (!py_func || !PyCallable_Check(py_func)) | |
134 | goto cleanup; | |
135 | ||
136 | /* Call into Python to format the stack trace. */ | |
137 | py_tracefmt = PyObject_CallFunctionObjArgs(py_func, | |
138 | py_etype, py_evalue, py_etraceback, NULL); | |
139 | if (!py_tracefmt) | |
140 | goto cleanup; | |
141 | ||
142 | tracefmt_str = py_stringify(py_tracefmt); | |
143 | Py_DECREF(py_tracefmt); | |
144 | ||
145 | /* Log the detailed stack trace. */ | |
146 | if (tracefmt_str) { | |
147 | srd_dbg("%s", tracefmt_str); | |
148 | g_free(tracefmt_str); | |
ec871a27 | 149 | } |
ec871a27 | 150 | |
201a85a8 DE |
151 | cleanup: |
152 | Py_XDECREF(py_func); | |
153 | Py_XDECREF(py_mod); | |
154 | Py_XDECREF(py_etraceback); | |
155 | Py_XDECREF(py_evalue); | |
156 | Py_XDECREF(py_etype); | |
ec871a27 BV |
157 | |
158 | /* Just in case. */ | |
159 | PyErr_Clear(); | |
201a85a8 | 160 | |
514b2edc UH |
161 | PyGILState_Release(gstate); |
162 | ||
201a85a8 | 163 | g_free(msg); |
ec871a27 | 164 | } |