-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-134170: Add colorization to unraisable exceptions #134183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e8ea2a3
36e38e6
6c9d619
d2e621a
411406c
4e9a0b6
19db581
6e57c96
06d43dd
ca0efb4
bcfd811
b329019
8a325d3
2360427
9a1d88e
a8cf99a
b32084c
79fb9d2
4c339f3
2793964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add colorization to unraisable exceptions by default | ||
(:func:`sys.unraisablehook`). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1445,12 +1445,14 @@ make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type, | |
|
||
It can be called to log the exception of a custom sys.unraisablehook. | ||
|
||
Do nothing if sys.stderr attribute doesn't exist or is set to None. */ | ||
This assumes file is non-NULL. | ||
*/ | ||
static int | ||
write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type, | ||
PyObject *exc_value, PyObject *exc_tb, | ||
PyObject *err_msg, PyObject *obj, PyObject *file) | ||
{ | ||
assert(file != NULL && !Py_IsNone(file)); | ||
if (obj != NULL && obj != Py_None) { | ||
if (err_msg != NULL && err_msg != Py_None) { | ||
if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) { | ||
|
@@ -1485,6 +1487,24 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type, | |
} | ||
} | ||
|
||
// Try printing the exception using the stdlib module. | ||
// If this fails, then we have to use the C implementation. | ||
PyObject *print_exception_fn = PyImport_ImportModuleAttrString("traceback", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would print the exception with color on stderr but
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just modified There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the comment for this function is:
So I don't really know which one is the correct implementation. There are cases when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, what am I checking for? If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, yeah, I think that comment is wrong. Should I change it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes (wrong comments are misleading!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did that and added an assertion. The caller always validates |
||
"_print_exception_bltin"); | ||
if (print_exception_fn != NULL && PyCallable_Check(print_exception_fn)) { | ||
PyObject *args[2] = {exc_value, file}; | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyObject *result = PyObject_Vectorcall(print_exception_fn, args, 2, NULL); | ||
Py_DECREF(print_exception_fn); | ||
Py_XDECREF(result); | ||
if (result != NULL) { | ||
// Nothing else to do | ||
return 0; | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
// traceback module failed, fall back to pure C | ||
_PyErr_Clear(tstate); | ||
Py_XDECREF(print_exception_fn); | ||
|
||
if (exc_tb != NULL && exc_tb != Py_None) { | ||
if (PyTraceBack_Print(exc_tb, file) < 0) { | ||
/* continue even if writing the traceback failed */ | ||
|
Uh oh!
There was an error while loading. Please reload this page.