Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 1 | /* |
Uwe Hermann | 50bd5d2 | 2013-04-23 22:27:20 +0200 | [diff] [blame] | 2 | * This file is part of the libsigrokdecode project. |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 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 | |
Marcus Comstedt | f6c7ead | 2014-07-01 00:07:40 +0200 | [diff] [blame] | 20 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
| 21 | #include "libsigrokdecode.h" |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 22 | #include "config.h" |
| 23 | #include <stdarg.h> |
| 24 | #include <glib.h> |
| 25 | #include <frameobject.h> /* Python header not pulled in by default. */ |
| 26 | |
Uwe Hermann | 57790bc | 2013-02-09 21:44:11 +0100 | [diff] [blame] | 27 | /** @private */ |
Uwe Hermann | aafeeae | 2012-03-25 15:08:16 +0200 | [diff] [blame] | 28 | SRD_PRIV void srd_exception_catch(const char *format, ...) |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 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))) { |
Uwe Hermann | 7a1712c | 2012-01-26 01:15:10 +0100 | [diff] [blame] | 44 | /* Shouldn't happen. */ |
| 45 | srd_dbg("Failed to convert exception value to string."); |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
Bert Vermeulen | 84d7ecb | 2014-07-08 21:43:33 +0200 | [diff] [blame] | 49 | /* Send the exception error message(s) to srd_err(). */ |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 50 | if (evalue) |
| 51 | ename = (char *)Py_TYPE(evalue)->tp_name; |
| 52 | else |
Bert Vermeulen | 84d7ecb | 2014-07-08 21:43:33 +0200 | [diff] [blame] | 53 | /* Can be NULL. */ |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 54 | ename = "(unknown exception)"; |
| 55 | |
Bert Vermeulen | 84d7ecb | 2014-07-08 21:43:33 +0200 | [diff] [blame] | 56 | msg = g_string_sized_new(128); |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 57 | g_string_append(msg, ename); |
| 58 | g_string_append(msg, ": "); |
Bert Vermeulen | 84d7ecb | 2014-07-08 21:43:33 +0200 | [diff] [blame] | 59 | va_start(args, format); |
| 60 | g_string_append_vprintf(msg, format, args); |
| 61 | va_end(args); |
| 62 | py_str_as_str(py_str, &str); |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 63 | g_string_append(msg, str); |
| 64 | Py_DecRef(py_str); |
| 65 | srd_err(msg->str); |
| 66 | |
| 67 | /* Send a more precise error location to srd_dbg(), if we have it. */ |
| 68 | if (etb && etb != Py_None) { |
| 69 | tracestr = NULL; |
Uwe Hermann | 361fdca | 2012-03-15 22:00:24 +0100 | [diff] [blame] | 70 | py_tb = (PyTracebackObject *)etb; |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 71 | py_str = PyUnicode_FromFormat("%U:%d in %U", |
Uwe Hermann | c9bfccc | 2012-02-08 22:39:30 +0100 | [diff] [blame] | 72 | py_tb->tb_frame->f_code->co_filename, |
| 73 | py_tb->tb_frame->f_lineno, |
| 74 | py_tb->tb_frame->f_code->co_name); |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 75 | py_str_as_str(py_str, &tracestr); |
| 76 | Py_DecRef(py_str); |
Uwe Hermann | 7a1712c | 2012-01-26 01:15:10 +0100 | [diff] [blame] | 77 | g_string_printf(msg, "%s in %s: %s", ename, tracestr, str); |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 78 | srd_dbg(msg->str); |
| 79 | g_free(tracestr); |
| 80 | } |
| 81 | g_free(str); |
| 82 | g_string_free(msg, TRUE); |
| 83 | |
| 84 | Py_XDECREF(etype); |
| 85 | Py_XDECREF(evalue); |
| 86 | Py_XDECREF(etb); |
| 87 | |
| 88 | /* Just in case. */ |
| 89 | PyErr_Clear(); |
Bert Vermeulen | ec871a2 | 2012-01-23 02:20:51 +0100 | [diff] [blame] | 90 | } |