Skip to content

Commit e8ea2a3

Browse files
committed
Add colorization to unraisable exceptions.
1 parent 0a160bf commit e8ea2a3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Python/errors.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,8 +1439,6 @@ make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
14391439
return args;
14401440
}
14411441

1442-
1443-
14441442
/* Default implementation of sys.unraisablehook.
14451443
14461444
It can be called to log the exception of a custom sys.unraisablehook.
@@ -1485,6 +1483,20 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
14851483
}
14861484
}
14871485

1486+
// Try printing the exception with color
1487+
PyObject *print_exception_fn = PyImport_ImportModuleAttrString("traceback",
1488+
"_print_exception_bltin");
1489+
if (print_exception_fn != NULL && PyCallable_Check(print_exception_fn)) {
1490+
PyObject *result = PyObject_CallOneArg(print_exception_fn, exc_value);
1491+
Py_DECREF(print_exception_fn);
1492+
Py_XDECREF(result);
1493+
if (result != NULL) {
1494+
return 0;
1495+
}
1496+
}
1497+
// traceback module failed, fall back to pure C
1498+
Py_XDECREF(print_exception_fn);
1499+
14881500
if (exc_tb != NULL && exc_tb != Py_None) {
14891501
if (PyTraceBack_Print(exc_tb, file) < 0) {
14901502
/* continue even if writing the traceback failed */

0 commit comments

Comments
 (0)