-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Add colorization of exceptions in default sys.unraisablehook
#134170
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
Comments
This is because the "Exception [was] ignored in atexit callback," meaning it only appears after you exit the repl, which therefore means it is not colored. I do not find this too surprising. cc @ambv should this be closed? |
Yes, in this case I don't think the REPL is able to re-add colorization. It's too late IMO. |
We could totally add colorization here! Unraisable exceptions simply invoke Here's a super rough POC: import traceback
import sys
import atexit
def hook(unraisable):
exc = unraisable.exc_value
print(unraisable.err_msg, file=sys.stderr)
traceback.print_exception(exc, colorize=True)
sys.unraisablehook = hook
def foo():
raise Exception('foo')
atexit.register(foo) Ideally, we'd just do this in C by calling into Line 1489 in 0a160bf
|
Shouldn't we chain the unraisable hooks then? what if someone already has a hook registered? |
It wouldn't be any different than someone overwriting |
Hum, maybe I misunderstood, but do you suggest we always colorize exceptions? wouldn't it be problematic to rely on colorization which is implemented in a Python file and could fail at exit? (namely, what happens if an error occurs during the colorization?) |
We do always colorize exceptions in 3.13+, and fall back to the C implementation of |
Is it safe to do it inside? namely is it safe to use this complex logic inside unraisablehook or not? if it's the case, I'm fine with this! |
Yeah, it's completely safe. |
sys.unraisablehook
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
There are no colored exceptions in atexit module. Like this:
Python 3.13.3
Windows 10
CPython versions tested on:
3.13
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: