-
- PyErr_Fetch(&type, &value, &traceback);
-
- BT_ASSERT(type != NULL);
-
- /*
- * traceback can be NULL, when we fail to call a Python function from the
- * native code (there is no Python stack at that point). E.g.:
- *
- * TypeError: _query_from_native() takes 5 positional arguments but 8 were given
- */
-
-
- /* Make sure `value` is what we expected - an instance of `type`. */
- PyErr_NormalizeException(&type, &value, &traceback);
-
- traceback_module = PyImport_ImportModule("traceback");
- if (!traceback_module) {
- BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
- goto end;
- }
-
- format_exception_func = PyObject_GetAttrString(traceback_module,
- traceback ? "format_exception" : "format_exception_only");
- if (!format_exception_func) {
- BT_LOGE_STR("Failed to log Python exception (could not find format_exception).");
- goto end;
- }
-
- if (!PyCallable_Check(format_exception_func)) {
- BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
- goto end;
- }
-
- exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
- if (!exc_str_list) {
- PyErr_Print();
- BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");