On 04/05/2018 07:47 AM, Richard W.M. Jones wrote:
---
configure.ac | 8 +++++++
plugins/python/python.c | 51 +++++++++++++++++++++++++++++++++++++++---
tests/Makefile.am | 1 +
tests/python-exception.py | 45 +++++++++++++++++++++++++++++++++++++
tests/test-python-exception.sh | 42 ++++++++++++++++++++++++++++++++++
5 files changed, 144 insertions(+), 3 deletions(-)
+/* Convert bytes/str/unicode into a string. Caller must free. */
+static char *
+python_to_string (PyObject *str)
+{
+ char *r;
+
+ if (str) {
+#ifdef HAVE_PYUNICODE_ASUTF8
+ if (PyUnicode_Check (str)) {
+ r = PyUnicode_AsUTF8 (str);
+ r = strdup (r);
+ return r;
Any simpler to just write:
return strdup (PyUnicode_AsUTF8 (str));
instead of using the temporary variable r?
static int
check_python_failure (const char *callback)
{
if (PyErr_Occurred ()) {
- nbdkit_error ("%s: callback failed: %s", script, callback);
- /* How to turn this into a string? XXX */
- PyErr_Print ();
+ PyObject *type, *error, *traceback, *error_str;
+ char *error_cstr;
+
+ /* Convert the Python exception to a string.
+ *
https://stackoverflow.com/a/1418703
+ * But forget about the traceback, it's very hard to print.
+ *
https://stackoverflow.com/q/1796510
+ */
+ PyErr_Fetch (&type, &error, &traceback);
+ PyErr_NormalizeException (&type, &error, &traceback);
+ error_str = PyObject_Str (error);
+ error_cstr = python_to_string (error_str);
+ nbdkit_error ("%s: %s: error: %s",
+ script, callback, error_cstr ? : "<unknown>");
Okay, not the first use of that gcc extension.
Looks reasonable to me.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization:
qemu.org |
libvirt.org