-
-
Notifications
You must be signed in to change notification settings - Fork 31k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-117225: Add color to doctest output #117583
Merged
Merged
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
2c1108b
Add colour to doctest output and create _colorize module
hugovk d27c0a8
Use _colorize in traceback module
hugovk bb591b6
Fix whitespace
hugovk 42079be
Use f-strings
hugovk 0088579
Remove underscores from members of an underscored module
hugovk d3034fa
Add blurb
hugovk 39780cb
Remove underscores from members of an underscored module
hugovk c5aec15
Revert "Fix whitespace"
hugovk 7e40133
Move _colorize to stdlib block, colour->color
hugovk e484465
Move imports together
hugovk 1c7b025
Move imports together
hugovk ab2c94c
Move imports together
hugovk 1aaeab8
Revert notests -> no_tests
hugovk cd02e4a
Revert "Use f-strings"
hugovk 06543ff
Fix local tests
hugovk 31c6647
Use red divider for failed test
hugovk 9be3d81
Fix local tests
hugovk e4ff3e3
Less red
hugovk b62500a
Revert unnecessary changes
hugovk eb4f8dc
Move colour tests to test__colorize.py
hugovk 976bfb4
Refactor asserts
hugovk ad7a946
Add missing captured_output to test.support's __all__ to fix IDE warning
hugovk 796e9f2
Only move test_colorized_detection_checks_for_environment_variables f…
hugovk 99d4d0c
Apply suggestions from code review
hugovk 95b9831
Use unittest's enterContext
hugovk d5417b4
Merge remote-tracking branch 'upstream/main' into doctest-tidy-output…
hugovk ece3ce0
Keep colorize functionality in traceback module for now
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import io | ||
import os | ||
import sys | ||
|
||
COLORIZE = True | ||
|
||
|
||
class ANSIColors: | ||
BOLD_GREEN = "\x1b[1;32m" | ||
BOLD_MAGENTA = "\x1b[1;35m" | ||
BOLD_RED = "\x1b[1;31m" | ||
GREEN = "\x1b[32m" | ||
GREY = "\x1b[90m" | ||
MAGENTA = "\x1b[35m" | ||
RED = "\x1b[31m" | ||
RESET = "\x1b[0m" | ||
YELLOW = "\x1b[33m" | ||
|
||
|
||
def can_colorize(): | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if sys.platform == "win32": | ||
try: | ||
import nt | ||
|
||
if not nt._supports_virtual_terminal(): | ||
return False | ||
except (ImportError, AttributeError): | ||
return False | ||
|
||
if os.environ.get("PYTHON_COLORS") == "0": | ||
return False | ||
if os.environ.get("PYTHON_COLORS") == "1": | ||
return True | ||
if "NO_COLOR" in os.environ: | ||
return False | ||
if not COLORIZE: | ||
return False | ||
if "FORCE_COLOR" in os.environ: | ||
return True | ||
if os.environ.get("TERM") == "dumb": | ||
return False | ||
try: | ||
return os.isatty(sys.stderr.fileno()) | ||
except io.UnsupportedOperation: | ||
return sys.stderr.isatty() |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import contextlib | ||
import sys | ||
import traceback | ||
import unittest | ||
import unittest.mock | ||
import _colorize | ||
from test.support import captured_output | ||
|
||
|
||
class TestColorizedTraceback(unittest.TestCase): | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_colorized_detection_checks_for_environment_variables(self): | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if sys.platform == "win32": | ||
virtual_patching = unittest.mock.patch( | ||
"nt._supports_virtual_terminal", return_value=True | ||
) | ||
else: | ||
virtual_patching = contextlib.nullcontext() | ||
|
||
env_vars_expected = [ | ||
({'TERM': 'dumb'}, False), | ||
({'PYTHON_COLORS': '1'}, True), | ||
({'PYTHON_COLORS': '0'}, False), | ||
({'NO_COLOR': '1'}, False), | ||
({'NO_COLOR': '1', "PYTHON_COLORS": '1'}, True), | ||
({'FORCE_COLOR': '1'}, True), | ||
({'FORCE_COLOR': '1', 'NO_COLOR': '1'}, False), | ||
({'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}, False), | ||
] | ||
|
||
with virtual_patching: | ||
with unittest.mock.patch("os.isatty") as isatty_mock: | ||
isatty_mock.return_value = True | ||
|
||
for env_vars, expected in env_vars_expected: | ||
with unittest.mock.patch("os.environ", env_vars): | ||
self.assertEqual(_colorize.can_colorize(), expected) | ||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
isatty_mock.return_value = False | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(_colorize.can_colorize(), False) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
get_ansi_colors()
preserves IDE suggestions (ctrl+space) and centralizes, as well as abstracts color availability logic, improving code's clarity and flexibility.