Skip to content

IDLE displays ANSI sequences in tracebacks when sys.excepthook is customized #127060

Description

@vstinner

Error on Python 3.13 and newer.

Reproducer

  • Run IDLE
  • Set a custom sys.excepthook:
import sys
def hook(*args): return sys.__excepthook__(*args)
sys.excepthook=hook
  • Trigger an error which display a traceback, such as accessing a variable which doesn't exist.

Output

>>> os
Traceback (most recent call last):
  File �[35m"/home/vstinner/python/3.13/Lib/idlelib/run.py"�[0m, line �[35m590�[0m, in �[35mruncode�[0m
    �[31mexec�[0m�[1;31m(code, self.locals)�[0m
    �[31m~~~~�[0m�[1;31m^^^^^^^^^^^^^^^^^^^�[0m
  File �[35m"<pyshell#12>"�[0m, line �[35m1�[0m, in �[35m<module>�[0m
�[1;35mNameError�[0m: �[35mname 'os' is not defined. Did you forget to import 'os'?�[0m

ANSI sequences to colorize the output are unexpected and makes the output hard to read.

Expected output

>>> os
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    os
NameError: name 'os' is not defined. Did you forget to import 'os'?

Explanation

In the normal case:

  • IDLE replaces sys.stderr with io.StringIO() to call the "excepthook".
  • _colorize.can_colorize() returns False.
  • It works as expected.

If sys.excepthook is overridden:

  • IDLE replaces sys.stderr with a idlelib.run.StdOutputFile object at startup.
  • _colorize.can_colorize() returns True because StdOutputFile.isatty() always return True.

Linked PRs

Activity

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

    3.13bugs and security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-IDLEtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions