]> sigrok.org Git - libsigrokdecode.git/blame - exception.c
Doxygen: Don't parse anything in tests/ or doxy/.
[libsigrokdecode.git] / exception.c
CommitLineData
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
c1f86f02
UH
20#include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21#include "libsigrokdecode-internal.h"
ec871a27
BV
22#include "config.h"
23#include <stdarg.h>
24#include <glib.h>
25#include <frameobject.h> /* Python header not pulled in by default. */
26
57790bc8 27/** @private */
aafeeaea 28SRD_PRIV void srd_exception_catch(const char *format, ...)
ec871a27
BV
29{
30 PyObject *etype, *evalue, *etb, *py_str;
31 PyTracebackObject *py_tb;
32 GString *msg;
33 va_list args;
34 char *ename, *str, *tracestr;
35
36 if (!PyErr_Occurred())
37 /* Nothing is wrong. */
38 return;
39
40 PyErr_Fetch(&etype, &evalue, &etb);
41 PyErr_NormalizeException(&etype, &evalue, &etb);
42
43 if (!(py_str = PyObject_Str(evalue))) {
7a1712c4
UH
44 /* Shouldn't happen. */
45 srd_dbg("Failed to convert exception value to string.");
ec871a27
BV
46 return;
47 }
48
49 msg = g_string_sized_new(128);
50 va_start(args, format);
51 g_string_vprintf(msg, format, args);
52 va_end(args);
53
54 /* Can be NULL. */
55 if (evalue)
56 ename = (char *)Py_TYPE(evalue)->tp_name;
57 else
58 ename = "(unknown exception)";
59
60 /* Send the exception error message(s) to srd_err(). */
61 py_str_as_str(py_str, &str);
62 g_string_append(msg, ename);
63 g_string_append(msg, ": ");
64 g_string_append(msg, str);
65 Py_DecRef(py_str);
66 srd_err(msg->str);
67
68 /* Send a more precise error location to srd_dbg(), if we have it. */
69 if (etb && etb != Py_None) {
70 tracestr = NULL;
361fdcaa 71 py_tb = (PyTracebackObject *)etb;
ec871a27 72 py_str = PyUnicode_FromFormat("%U:%d in %U",
c9bfccc6
UH
73 py_tb->tb_frame->f_code->co_filename,
74 py_tb->tb_frame->f_lineno,
75 py_tb->tb_frame->f_code->co_name);
ec871a27
BV
76 py_str_as_str(py_str, &tracestr);
77 Py_DecRef(py_str);
7a1712c4 78 g_string_printf(msg, "%s in %s: %s", ename, tracestr, str);
ec871a27
BV
79 srd_dbg(msg->str);
80 g_free(tracestr);
81 }
82 g_free(str);
83 g_string_free(msg, TRUE);
84
85 Py_XDECREF(etype);
86 Py_XDECREF(evalue);
87 Py_XDECREF(etb);
88
89 /* Just in case. */
90 PyErr_Clear();
ec871a27 91}