Skip to content

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

Open
gry321 opened this issue May 18, 2025 · 9 comments
Open

Add colorization of exceptions in default sys.unraisablehook #134170

gry321 opened this issue May 18, 2025 · 9 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@gry321
Copy link

gry321 commented May 18, 2025

Bug report

Bug description:

There are no colored exceptions in atexit module. Like this:

import atexit
def foo():
    raise Exception('foo')
atexit.register(foo)
Exception ignored in atexit callback <function foo at 0x00000114C7D5C720>:
Traceback (most recent call last):
  File "...", line 3, in foo
    raise Exception('foo')
Exception: foo

Python 3.13.3
Windows 10

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@gry321 gry321 added the type-bug An unexpected behavior, bug, or error label May 18, 2025
@StanFromIreland
Copy link
Contributor

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 picnixz added type-feature A feature request or enhancement stdlib Python modules in the Lib dir pending The issue will be closed if no feedback is provided and removed type-bug An unexpected behavior, bug, or error labels May 18, 2025
@picnixz
Copy link
Member

picnixz commented 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
Copy link
Member

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:

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 traceback._print_exception_bltin. The relevant line is here:

if (PyTraceBack_Print(exc_tb, file) < 0) {

@ZeroIntensity ZeroIntensity removed the pending The issue will be closed if no feedback is provided label May 18, 2025
@picnixz
Copy link
Member

picnixz commented May 18, 2025

Shouldn't we chain the unraisable hooks then? what if someone already has a hook registered?

@ZeroIntensity
Copy link
Member

ZeroIntensity commented May 18, 2025

It wouldn't be any different than someone overwriting sys.excepthook.

@picnixz
Copy link
Member

picnixz commented 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
Copy link
Member

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.

@picnixz
Copy link
Member

picnixz commented May 18, 2025

My proposal is to extend that behavior to sys.unraisablehook.

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
Copy link
Member

Yeah, it's completely safe. sys.unraisablehook can already call arbitrary Python code, so we're not really adding anything new in that sense.

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed stdlib Python modules in the Lib dir labels May 18, 2025
@picnixz picnixz changed the title No colored exceptions in atexit module Add colorization of exceptions in default sys.unraisablehook May 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy