Skip to content

Add colorization of exceptions in default sys.unraisablehook #134170

Open
@gry321

Description

@gry321

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

Activity

added
type-bugAn unexpected behavior, bug, or error
on May 18, 2025
StanFromIreland

StanFromIreland commented on May 18, 2025

@StanFromIreland
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?

added
type-featureA feature request or enhancement
stdlibPython modules in the Lib dir
pendingThe issue will be closed if no feedback is provided
and removed
type-bugAn unexpected behavior, bug, or error
on May 18, 2025
picnixz

picnixz commented on May 18, 2025

@picnixz
Member

Yes, in this case I don't think the REPL is able to re-add colorization. It's too late IMO.

ZeroIntensity

ZeroIntensity commented on May 18, 2025

@ZeroIntensity
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) {

removed
pendingThe issue will be closed if no feedback is provided
on May 18, 2025
picnixz

picnixz commented on May 18, 2025

@picnixz
Member

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

ZeroIntensity

ZeroIntensity commented on May 18, 2025

@ZeroIntensity
Member

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

picnixz

picnixz commented on May 18, 2025

@picnixz
Member

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

ZeroIntensity commented on May 18, 2025

@ZeroIntensity
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

picnixz commented on May 18, 2025

@picnixz
Member

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

ZeroIntensity commented on May 18, 2025

@ZeroIntensity
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.

4 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Add colorization of exceptions in default `sys.unraisablehook` · Issue #134170 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant