]>
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 | } | |
7969d803 | 53 | |
201a85a8 DE |
54 | return str; |
55 | } | |
56 | ||
57 | static char *py_get_string_attr(PyObject *py_obj, const char *attr) | |
58 | { | |
59 | PyObject *py_str, *py_bytes; | |
60 | char *str = NULL; | |
61 | ||
514b2edc UH |
62 | /* Note: Caller already ran PyGILState_Ensure(). */ |
63 | ||
201a85a8 DE |
64 | if (!py_obj) |
65 | return NULL; | |
66 | ||
67 | py_str = PyObject_GetAttrString(py_obj, attr); | |
68 | if (!py_str || !PyUnicode_Check(py_str)) | |
69 | goto cleanup; | |
70 | ||
71 | py_bytes = PyUnicode_AsUTF8String(py_str); | |
72 | if (!py_bytes) | |
73 | goto cleanup; | |
74 | ||
75 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
76 | Py_DECREF(py_bytes); | |
77 | ||
78 | cleanup: | |
79 | Py_XDECREF(py_str); | |
80 | if (!str) { | |
81 | PyErr_Clear(); | |
82 | srd_dbg("Failed to get object attribute %s.", attr); | |
83 | } | |
7969d803 | 84 | |
201a85a8 DE |
85 | return str; |
86 | } | |
ec871a27 | 87 | |
57790bc8 | 88 | /** @private */ |
aafeeaea | 89 | SRD_PRIV void srd_exception_catch(const char *format, ...) |
ec871a27 | 90 | { |
752086e9 | 91 | int i, ret; |
ec871a27 | 92 | va_list args; |
201a85a8 | 93 | PyObject *py_etype, *py_evalue, *py_etraceback; |
e9dd2fea | 94 | PyObject *py_mod, *py_func, *py_tracefmt; |
752086e9 | 95 | char *msg, *etype_name, *evalue_str, *outstr; |
201a85a8 | 96 | const char *etype_name_fallback; |
514b2edc | 97 | PyGILState_STATE gstate; |
752086e9 | 98 | GString *s; |
ec871a27 | 99 | |
201a85a8 | 100 | py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL; |
ec871a27 | 101 | |
201a85a8 DE |
102 | va_start(args, format); |
103 | msg = g_strdup_vprintf(format, args); | |
104 | va_end(args); | |
ec871a27 | 105 | |
514b2edc UH |
106 | gstate = PyGILState_Ensure(); |
107 | ||
201a85a8 DE |
108 | PyErr_Fetch(&py_etype, &py_evalue, &py_etraceback); |
109 | if (!py_etype) { | |
110 | /* No current exception, so just print the message. */ | |
111 | srd_err("%s.", msg); | |
112 | goto cleanup; | |
ec871a27 | 113 | } |
201a85a8 | 114 | PyErr_NormalizeException(&py_etype, &py_evalue, &py_etraceback); |
ec871a27 | 115 | |
201a85a8 DE |
116 | etype_name = py_get_string_attr(py_etype, "__name__"); |
117 | evalue_str = py_stringify(py_evalue); | |
118 | etype_name_fallback = (etype_name) ? etype_name : "(unknown exception)"; | |
119 | ||
120 | if (evalue_str) | |
121 | srd_err("%s: %s: %s", etype_name_fallback, msg, evalue_str); | |
ec871a27 | 122 | else |
201a85a8 | 123 | srd_err("%s: %s.", etype_name_fallback, msg); |
ec871a27 | 124 | |
201a85a8 DE |
125 | g_free(evalue_str); |
126 | g_free(etype_name); | |
127 | ||
128 | /* If there is no traceback object, we are done. */ | |
129 | if (!py_etraceback) | |
130 | goto cleanup; | |
131 | ||
e9dd2fea | 132 | py_mod = py_import_by_name("traceback"); |
201a85a8 DE |
133 | if (!py_mod) |
134 | goto cleanup; | |
135 | ||
136 | py_func = PyObject_GetAttrString(py_mod, "format_exception"); | |
137 | if (!py_func || !PyCallable_Check(py_func)) | |
138 | goto cleanup; | |
139 | ||
140 | /* Call into Python to format the stack trace. */ | |
141 | py_tracefmt = PyObject_CallFunctionObjArgs(py_func, | |
142 | py_etype, py_evalue, py_etraceback, NULL); | |
752086e9 | 143 | if (!py_tracefmt || !PyList_Check(py_tracefmt)) |
201a85a8 DE |
144 | goto cleanup; |
145 | ||
752086e9 UH |
146 | s = g_string_sized_new(128); |
147 | for (i = 0; i < PyList_Size(py_tracefmt); i++) { | |
148 | ret = py_listitem_as_str(py_tracefmt, i, &outstr); | |
149 | if (ret == 0) { | |
150 | s = g_string_append(s, outstr); | |
151 | g_free(outstr); | |
152 | } | |
ec871a27 | 153 | } |
752086e9 UH |
154 | srd_err("%s", s->str); |
155 | g_string_free(s, TRUE); | |
156 | ||
157 | Py_DECREF(py_tracefmt); | |
ec871a27 | 158 | |
201a85a8 DE |
159 | cleanup: |
160 | Py_XDECREF(py_func); | |
161 | Py_XDECREF(py_mod); | |
162 | Py_XDECREF(py_etraceback); | |
163 | Py_XDECREF(py_evalue); | |
164 | Py_XDECREF(py_etype); | |
ec871a27 BV |
165 | |
166 | /* Just in case. */ | |
167 | PyErr_Clear(); | |
201a85a8 | 168 | |
514b2edc UH |
169 | PyGILState_Release(gstate); |
170 | ||
201a85a8 | 171 | g_free(msg); |
ec871a27 | 172 | } |