]> sigrok.org Git - libsigrokdecode.git/blob - exception.c
spiflash: Add basic Adesto AT45DBxx support (WRITE1/2, STATUS).
[libsigrokdecode.git] / exception.c
1 /*
2  * This file is part of the libsigrokdecode project.
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
20 #include <config.h>
21 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "libsigrokdecode.h"
23 #include <stdarg.h>
24 #include <glib.h>
25
26 static char *py_stringify(PyObject *py_obj)
27 {
28         PyObject *py_str, *py_bytes;
29         char *str = NULL;
30
31         /* Note: Caller already ran PyGILState_Ensure(). */
32
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
61         /* Note: Caller already ran PyGILState_Ensure(). */
62
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 }
85
86 /** @private */
87 SRD_PRIV void srd_exception_catch(const char *format, ...)
88 {
89         int i, ret;
90         va_list args;
91         PyObject *py_etype, *py_evalue, *py_etraceback;
92         PyObject *py_mod, *py_func, *py_tracefmt;
93         char *msg, *etype_name, *evalue_str, *outstr;
94         const char *etype_name_fallback;
95         PyGILState_STATE gstate;
96         GString *s;
97
98         py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL;
99
100         va_start(args, format);
101         msg = g_strdup_vprintf(format, args);
102         va_end(args);
103
104         gstate = PyGILState_Ensure();
105
106         PyErr_Fetch(&py_etype, &py_evalue, &py_etraceback);
107         if (!py_etype) {
108                 /* No current exception, so just print the message. */
109                 srd_err("%s.", msg);
110                 goto cleanup;
111         }
112         PyErr_NormalizeException(&py_etype, &py_evalue, &py_etraceback);
113
114         etype_name = py_get_string_attr(py_etype, "__name__");
115         evalue_str = py_stringify(py_evalue);
116         etype_name_fallback = (etype_name) ? etype_name : "(unknown exception)";
117
118         if (evalue_str)
119                 srd_err("%s: %s: %s", etype_name_fallback, msg, evalue_str);
120         else
121                 srd_err("%s: %s.", etype_name_fallback, msg);
122
123         g_free(evalue_str);
124         g_free(etype_name);
125
126         /* If there is no traceback object, we are done. */
127         if (!py_etraceback)
128                 goto cleanup;
129
130         py_mod = py_import_by_name("traceback");
131         if (!py_mod)
132                 goto cleanup;
133
134         py_func = PyObject_GetAttrString(py_mod, "format_exception");
135         if (!py_func || !PyCallable_Check(py_func))
136                 goto cleanup;
137
138         /* Call into Python to format the stack trace. */
139         py_tracefmt = PyObject_CallFunctionObjArgs(py_func,
140                         py_etype, py_evalue, py_etraceback, NULL);
141         if (!py_tracefmt || !PyList_Check(py_tracefmt))
142                 goto cleanup;
143
144         s = g_string_sized_new(128);
145         for (i = 0; i < PyList_Size(py_tracefmt); i++) {
146                 ret = py_listitem_as_str(py_tracefmt, i, &outstr);
147                 if (ret == 0) {
148                         s = g_string_append(s, outstr);
149                         g_free(outstr);
150                 }
151         }
152         srd_err("%s", s->str);
153         g_string_free(s, TRUE);
154
155         Py_DECREF(py_tracefmt);
156
157 cleanup:
158         Py_XDECREF(py_func);
159         Py_XDECREF(py_mod);
160         Py_XDECREF(py_etraceback);
161         Py_XDECREF(py_evalue);
162         Py_XDECREF(py_etype);
163
164         /* Just in case. */
165         PyErr_Clear();
166
167         PyGILState_Release(gstate);
168
169         g_free(msg);
170 }