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'?�[0mANSI 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.stderrwithio.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.StdOutputFileobject at startup. _colorize.can_colorize()returnsTruebecauseStdOutputFile.isatty()always returnTrue.