plugins/python/python.c
/* Convert the Python traceback to a string and call nbdkit_error.
 * https://stackoverflow.com/a/15907460/7126113
 */
Convert the Python traceback to a string and call nbdkit_error.
Convert the Python traceback to a string and call nbdkit_error .
static int
print_python_traceback (const char *callback,
                        PyObject *type, PyObject *error, PyObject *traceback)
{
  PyObject *module_name, *traceback_module, *format_exception_fn, *rv,
    *traceback_str;
  char *traceback_cstr;
#ifdef HAVE_PYSTRING_FROMSTRING
  module_name = PyString_FromString ("traceback");
#else
  module_name = PyUnicode_FromString ("traceback");
#endif
  traceback_module = PyImport_Import (module_name);
  Py_DECREF (module_name);
  if (traceback_module == NULL)
    return -1;
  format_exception_fn = PyObject_GetAttrString (traceback_module,
                                                "format_exception");
  if (format_exception_fn == NULL)
    return -1;
  if (!PyCallable_Check (format_exception_fn))
    return -1;
  rv = PyObject_CallFunctionObjArgs (format_exception_fn,
                                     type, error, traceback, NULL);
  traceback_str = PyObject_Str (rv);
  Py_DECREF (rv);
  traceback_cstr = python_to_string (traceback_str);
  if (traceback_cstr == NULL) {
    Py_DECREF (traceback_str);
    return -1;
  }
  nbdkit_error ("%s: %s: error: %s",
                script, callback,
                traceback_cstr);
  Py_DECREF (traceback_str);
  free (traceback_cstr);
  return 0;
}
static int print_python_traceback ( const char * callback , PyObject * type , PyObject * error , PyObject * traceback ) { PyObject * module_name , * traceback_module , * format_exception_fn , * rv , * traceback_str ; char * traceback_cstr ; #ifdef HAVE_PYSTRING_FROMSTRING module_name = PyString_FromString ( " " ) ; #else module_name = PyUnicode_FromString ( " " ) ; #endif traceback_module = PyImport_Import ( module_name ) ; Py_DECREF ( module_name ) ; if ( traceback_module == NULL ) return -1 ; format_exception_fn = PyObject_GetAttrString ( traceback_module , " " ) ; if ( format_exception_fn == NULL ) return -1 ; if ( ! PyCallable_Check ( format_exception_fn ) ) return -1 ; rv = PyObject_CallFunctionObjArgs ( format_exception_fn , type , error , traceback , NULL ) ; traceback_str = PyObject_Str ( rv ) ; Py_DECREF ( rv ) ; traceback_cstr = python_to_string ( traceback_str ) ; if ( traceback_cstr == NULL ) { Py_DECREF ( traceback_str ) ; return -1 ; } nbdkit_error ( " " , script , callback , traceback_cstr ) ; Py_DECREF ( traceback_str ) ; free ( traceback_cstr ) ; return 0 ; }
Convert the Python traceback to a string and call nbdkit_error.
Convert the Python traceback to a string and call nbdkit_error .
/* couldn't 'import traceback' */ /* This means we succeeded in calling nbdkit_error. */