fix: don't color logged exceptions on non-TTY streams; add IPython support (Closes #87, #10) - #165
Open
18680368135 wants to merge 1 commit into
Open
Conversation
Previously, the logging integration used the global SUPPORTS_COLOR flag to decide whether to add ANSI color codes. This meant that if the terminal supported colors (or FORCE_COLOR=1 was set), exceptions logged to files or other non-TTY streams would contain escape sequences. This change introduces _stream_supports_color(stream) which checks both the global color support flag AND whether the specific output stream is a TTY. FORCE_COLOR=1 still acts as an explicit override that enables color on all streams. Fix Qix-#10: Add IPython/Jupyter support Add better_exceptions.integrations.ipython.install() which hooks into IPython's set_custom_exc API to replace the default traceback formatter with better_exceptions' formatter. SyntaxErrors are delegated to IPython's built-in handler since IPython has special formatting for them. Usage: from better_exceptions.integrations.ipython import install install() Tests: - test/test_logging_color.py: Tests for issue Qix-#87 - test/test_ipython_integration.py: Tests for issue Qix-#10 - Updated test_all.sh and regenerated expected output files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses two issues:
Fix #87: Logged exceptions shouldn't contain colour
Problem: When the terminal supports colors (or
FORCE_COLOR=1is set), exceptions logged through the logging module to non-TTY streams (e.g. files, pipes) would contain ANSI escape codes, making them hard to read in editors and log viewers.Solution: Introduced
_stream_supports_color(stream)which checks both the globalSUPPORTS_COLORflag and whether the specific output stream is a TTY.FORCE_COLOR=1still acts as an explicit override that enables color on all streams (for users who explicitly want colored logs).Changes to
better_exceptions/log.py:_stream_supports_color(stream)function that checksFORCE_COLOR,SUPPORTS_COLOR, and the stream's TTY status_make_logging_format_exception()to accept a stream parameter and use_stream_supports_color()at call timepatch()to pass the handler's stream to_make_logging_format_exception()Fix #10: ipython support
Problem: better_exceptions had no integration with IPython/Jupyter notebooks.
Solution: Added
better_exceptions.integrations.ipython.install()which hooks into IPython'sset_custom_excAPI to replace the default traceback formatter with better_exceptions' formatter.Features:
BaseExceptionsubclassesSyntaxErroris delegated to IPython's built-in handler (IPython has special formatting for syntax errors)Usage:
Changes
better_exceptions/log.py: Added_stream_supports_color()and modified_make_logging_format_exception()to accept stream parameterbetter_exceptions/integrations/ipython.py: New file implementing IPython integration viaset_custom_exctest/test_logging_color.py: New test file with 5 test cases for issue Logged exceptions shouldn't contain colour #87test/test_ipython_integration.py: New test file with 5 test cases for issue ipython support #10test_all.sh: Added both new test files to the test suitetest/output/*.out: Regenerated expected output files for all 12 test configurationsREADME.md: Added IPython/Jupyter usage sectionTesting
All tests pass locally across all 12 configurations (xterm/vt100/dumb × ascii/UTF-8 × color/nocolor):
New test files:
test/test_logging_color.py: Tests non-TTY stream color handling, FORCE_COLOR override, file handler outputtest/test_ipython_integration.py: Tests import, no active shell, mock shell registration, handler formatting, SyntaxError delegationChecklist
./test_all.shpasses all 12 configurations)Closes #87, #10