You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add colorization of exceptions in default sys.unraisablehook #134170
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.
We could totally add colorization here! Unraisable exceptions simply invoke sys.unraisablehook, which we can just modify to print the exception with color.
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 traceback when it's not available. My proposal is to extend that behavior to sys.unraisablehook.
#134183 seems to have caused a regression in a mypyc test case. AFAICT calling PyErr_WriteUnraisable now causes Exception ignore ... and the traceback to be emitted twice. The code generated by mypyc:
/* We may end up clearing live exceptions below, so make sure it's ours. */
assert(!PyErr_Occurred());
Anyway, if we ignore that, the underlying issue that mypyc needs to handle garbage collection during sys.unraisablehook correctly. This isn't limited to this patch; as I said, sys.unraisablehook could always call arbitrary Python code, and now we're just making it do that. mypyc needs to be able to prevent double-destruction in these cases.
We have some example code for tp_deallochere. I think there are two problems at hand:
The object is seemingly never untracked from the garbage collector. So, when those gc.collect() calls inside sys.unraisablehook will incorrectly think that the object needs to be destroyed. This isn't a problem after tp_free (or really, PyObject_GC_Del) because that will untrack it for you.
It looks like resurrection isn't handled properly. Use PyObject_CallFinalizerFromDealloc for this. If something else resurrects it (such as the garbage collection), it needs to take responsibility for freeing the object.
Activity
StanFromIreland commentedon May 18, 2025
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?
picnixz commentedon May 18, 2025
Yes, in this case I don't think the REPL is able to re-add colorization. It's too late IMO.
ZeroIntensity commentedon May 18, 2025
We could totally add colorization here! Unraisable exceptions simply invoke
sys.unraisablehook, which we can just modify to print the exception with color.Here's a super rough POC:
Ideally, we'd just do this in C by calling into
traceback._print_exception_bltin. The relevant line is here:cpython/Python/errors.c
Line 1489 in 0a160bf
picnixz commentedon May 18, 2025
Shouldn't we chain the unraisable hooks then? what if someone already has a hook registered?
ZeroIntensity commentedon May 18, 2025
It wouldn't be any different than someone overwriting
sys.excepthook.picnixz commentedon May 18, 2025
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?)
ZeroIntensity commentedon May 18, 2025
We do always colorize exceptions in 3.13+, and fall back to the C implementation of
tracebackwhen it's not available. My proposal is to extend that behavior tosys.unraisablehook.picnixz commentedon May 18, 2025
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!
ZeroIntensity commentedon May 18, 2025
Yeah, it's completely safe.
sys.unraisablehookcan already call arbitrary Python code, so we're not really adding anything new in that sense.[-]No colored exceptions in atexit module[/-][+]Add colorization of exceptions in default `sys.unraisablehook`[/+]gh-134170: Add colorization to unraisable exceptions (#134183)
ZeroIntensity commentedon Aug 4, 2025
All done, thanks for the suggestions @gry321
cdce8p commentedon Aug 10, 2025
#134183 seems to have caused a regression in a mypyc test case. AFAICT calling
PyErr_WriteUnraisablenow causesException ignore ...and the traceback to be emitted twice. The code generated by mypyc:This was the mypyc test command. Note it still uses
PyErr_FetchandPyErr_Restorebut the output is the same.ZeroIntensity commentedon Aug 10, 2025
Hi, thanks for the heads up. I think this is a problem in mypyc.
On a debug build, I see an assertion failure:
This is because it's not always safe to call
_PyType_Lookupwith an exception set:cpython/Objects/typeobject.c
Lines 6092 to 6093 in 046a4e3
Anyway, if we ignore that, the underlying issue that mypyc needs to handle garbage collection during
sys.unraisablehookcorrectly. This isn't limited to this patch; as I said,sys.unraisablehookcould always call arbitrary Python code, and now we're just making it do that. mypyc needs to be able to prevent double-destruction in these cases.Here's a repro:
If you add a
print()call to__del__, you'll see that it's called twice. In C, this is how mypyc is defining the__del__:We have some example code for
tp_deallochere. I think there are two problems at hand:gc.collect()calls insidesys.unraisablehookwill incorrectly think that the object needs to be destroyed. This isn't a problem aftertp_free(or really,PyObject_GC_Del) because that will untrack it for you.PyObject_CallFinalizerFromDeallocfor this. If something else resurrects it (such as the garbage collection), it needs to take responsibility for freeing the object.I'll submit an issue to mypyc about this.
pythongh-134170: Add colorization to unraisable exceptions (python#13…