]>
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 | ||
31 | if (!py_obj) | |
32 | return NULL; | |
33 | ||
34 | py_str = PyObject_Str(py_obj); | |
35 | if (!py_str || !PyUnicode_Check(py_str)) | |
36 | goto cleanup; | |
37 | ||
38 | py_bytes = PyUnicode_AsUTF8String(py_str); | |
39 | if (!py_bytes) | |
40 | goto cleanup; | |
41 | ||
42 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
43 | Py_DECREF(py_bytes); | |
44 | ||
45 | cleanup: | |
46 | Py_XDECREF(py_str); | |
47 | if (!str) { | |
48 | PyErr_Clear(); | |
49 | srd_dbg("Failed to stringify object."); | |
50 | } | |
51 | return str; | |
52 | } | |
53 | ||
54 | static char *py_get_string_attr(PyObject *py_obj, const char *attr) | |
55 | { | |
56 | PyObject *py_str, *py_bytes; | |
57 | char *str = NULL; | |
58 | ||
59 | if (!py_obj) | |
60 | return NULL; | |
61 | ||
62 | py_str = PyObject_GetAttrString(py_obj, attr); | |
63 | if (!py_str || !PyUnicode_Check(py_str)) | |
64 | goto cleanup; | |
65 | ||
66 | py_bytes = PyUnicode_AsUTF8String(py_str); | |
67 | if (!py_bytes) | |
68 | goto cleanup; | |
69 | ||
70 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
71 | Py_DECREF(py_bytes); | |
72 | ||
73 | cleanup: | |
74 | Py_XDECREF(py_str); | |
75 | if (!str) { | |
76 | PyErr_Clear(); | |
77 | srd_dbg("Failed to get object attribute %s.", attr); | |
78 | } | |
79 | return str; | |
80 | } | |
ec871a27 | 81 | |
57790bc8 | 82 | /** @private */ |
aafeeaea | 83 | SRD_PRIV void srd_exception_catch(const char *format, ...) |
ec871a27 | 84 | { |
ec871a27 | 85 | va_list args; |
201a85a8 | 86 | PyObject *py_etype, *py_evalue, *py_etraceback; |
e9dd2fea | 87 | PyObject *py_mod, *py_func, *py_tracefmt; |
201a85a8 DE |
88 | char *msg, *etype_name, *evalue_str, *tracefmt_str; |
89 | const char *etype_name_fallback; | |
ec871a27 | 90 | |
201a85a8 | 91 | py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL; |
ec871a27 | 92 | |
201a85a8 DE |
93 | va_start(args, format); |
94 | msg = g_strdup_vprintf(format, args); | |
95 | va_end(args); | |
ec871a27 | 96 | |
201a85a8 DE |
97 | PyErr_Fetch(&py_etype, &py_evalue, &py_etraceback); |
98 | if (!py_etype) { | |
99 | /* No current exception, so just print the message. */ | |
100 | srd_err("%s.", msg); | |
101 | goto cleanup; | |
ec871a27 | 102 | } |
201a85a8 | 103 | PyErr_NormalizeException(&py_etype, &py_evalue, &py_etraceback); |
ec871a27 | 104 | |
201a85a8 DE |
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)"; | |
108 | ||
109 | if (evalue_str) | |
110 | srd_err("%s: %s: %s", etype_name_fallback, msg, evalue_str); | |
ec871a27 | 111 | else |
201a85a8 | 112 | srd_err("%s: %s.", etype_name_fallback, msg); |
ec871a27 | 113 | |
201a85a8 DE |
114 | g_free(evalue_str); |
115 | g_free(etype_name); | |
116 | ||
117 | /* If there is no traceback object, we are done. */ | |
118 | if (!py_etraceback) | |
119 | goto cleanup; | |
120 | ||
e9dd2fea | 121 | py_mod = py_import_by_name("traceback"); |
201a85a8 DE |
122 | if (!py_mod) |
123 | goto cleanup; | |
124 | ||
125 | py_func = PyObject_GetAttrString(py_mod, "format_exception"); | |
126 | if (!py_func || !PyCallable_Check(py_func)) | |
127 | goto cleanup; | |
128 | ||
129 | /* Call into Python to format the stack trace. */ | |
130 | py_tracefmt = PyObject_CallFunctionObjArgs(py_func, | |
131 | py_etype, py_evalue, py_etraceback, NULL); | |
132 | if (!py_tracefmt) | |
133 | goto cleanup; | |
134 | ||
135 | tracefmt_str = py_stringify(py_tracefmt); | |
136 | Py_DECREF(py_tracefmt); | |
137 | ||
138 | /* Log the detailed stack trace. */ | |
139 | if (tracefmt_str) { | |
140 | srd_dbg("%s", tracefmt_str); | |
141 | g_free(tracefmt_str); | |
ec871a27 | 142 | } |
ec871a27 | 143 | |
201a85a8 DE |
144 | cleanup: |
145 | Py_XDECREF(py_func); | |
146 | Py_XDECREF(py_mod); | |
147 | Py_XDECREF(py_etraceback); | |
148 | Py_XDECREF(py_evalue); | |
149 | Py_XDECREF(py_etype); | |
ec871a27 BV |
150 | |
151 | /* Just in case. */ | |
152 | PyErr_Clear(); | |
201a85a8 DE |
153 | |
154 | g_free(msg); | |
ec871a27 | 155 | } |