Skip to content

gh-157581: can_colorize should handle unexpected exceptions - #157590

Open
edvilme wants to merge 3 commits into
python:mainfrom
edvilme:gh-157581-pyrepl-colorize-exception
Open

edvilme wants to merge 3 commits into
python:mainfrom
edvilme:gh-157581-pyrepl-colorize-exception

Conversation

@edvilme

@edvilme edvilme commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Before

image

After

image

This pull request improves the robustness of the _colorize module by making the colorization detection logic more resilient to unexpected exceptions when checking if a file is a TTY. It also adds a new test to ensure this behavior is correctly handled when files are closed.

Robustness improvements:

  • Updated can_colorize logic in _colorize.py to catch all exceptions (not just OSError) when checking if a file is a TTY, returning False if any exception occurs. This prevents crashes in edge cases, such as when a file is closed.

Test coverage enhancements:

  • Added a test in test__colorize.py to verify that can_colorize returns False for closed files, ensuring correct behavior for files that raise ValueError on fileno().
  • Imported tempfile in test__colorize.py to support the new test case for closed files.

Handle unexpected exceptions in can_colorize instead of surfacing them
in the REPL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant