-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-117225: Add color to doctest output #117583
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
Merged
Merged
Changes from 1 commit
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
8000
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
Move colour tests to test__colorize.py
- Loading branch information
commit eb4f8dc23fdf148f9d63e6c6b0d3831486c7e2f7
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import contextlib | ||
import sys | ||
import traceback | ||
import unittest | ||
import unittest.mock | ||
import _colorize | ||
from test.support import captured_output | ||
|
||
|
||
class TestColorizedTraceback(unittest.TestCase): | ||
def test_colorized_traceback(self): | ||
def foo(*args): | ||
x = {'a':{'b': None}} | ||
y = x['a']['b']['c'] | ||
|
||
def baz(*args): | ||
return foo(1,2,3,4) | ||
|
||
def bar(): | ||
return baz(1, | ||
2,3 | ||
,4) | ||
try: | ||
bar() | ||
except Exception as e: | ||
exc = traceback.TracebackException.from_exception( | ||
e, capture_locals=True | ||
) | ||
lines = "".join(exc.format(colorize=True)) | ||
red = _colorize.ANSIColors.RED | ||
boldr = _colorize.ANSIColors.BOLD_RED | ||
reset = _colorize.ANSIColors.RESET | ||
self.assertIn("y = " + red + "x['a']['b']" + reset + boldr + "['c']" + reset, lines) | ||
self.assertIn("return " + red + "foo" + reset + boldr + "(1,2,3,4)" + reset, lines) | ||
self.assertIn("return " + red + "baz" + reset + boldr + "(1," + reset, lines) | ||
self.assertIn(boldr + "2,3" + reset, lines) | ||
self.assertIn(boldr + ",4)" + reset, lines) | ||
self.assertIn(red + "bar" + reset + boldr + "()" + reset, lines) | ||
|
||
def test_colorized_syntax_error(self): | ||
try: | ||
compile("a $ b", "<string>", "exec") | ||
except SyntaxError as e: | ||
exc = traceback.TracebackException.from_exception( | ||
e, capture_locals=True | ||
) | ||
actual = "".join(exc.format(colorize=True)) | ||
magenta = _colorize.ANSIColors.MAGENTA | ||
boldm = _colorize.ANSIColors.BOLD_MAGENTA | ||
boldr = _colorize.ANSIColors.BOLD_RED | ||
reset = _colorize.ANSIColors.RESET | ||
expected = "".join( | ||
[ | ||
f' File {magenta}"<string>"{reset}, line {magenta}1{reset}\n', | ||
f' a {boldr}${reset} b\n', | ||
f' {boldr}^{reset}\n', | ||
f'{boldm}SyntaxError{reset}: {magenta}invalid syntax{reset}\n', | ||
] | ||
) | ||
self.assertIn(expected, actual) | ||
|
||
def test_colorized_traceback_is_the_default(self): | ||
def foo(): | ||
1/0 | ||
|
||
from _testcapi import exception_print | ||
|
||
try: | ||
foo() | ||
self.fail("No exception thrown.") | ||
except Exception as e: | ||
with captured_output("stderr") as tbstderr: | ||
with unittest.mock.patch('_colorize.can_colorize', return_value=True): | ||
exception_print(e) | ||
actual = tbstderr.getvalue().splitlines() | ||
|
||
red = _colorize.ANSIColors.RED | ||
boldr = _colorize.ANSIColors.BOLD_RED | ||
magenta = _colorize.ANSIColors.MAGENTA | ||
boldm = _colorize.ANSIColors.BOLD_MAGENTA | ||
reset = _colorize.ANSIColors.RESET | ||
lno_foo = foo.__code__.co_firstlineno | ||
expected = [ | ||
'Traceback (most recent call last):', | ||
f' File {magenta}"{__file__}"{reset}, ' | ||
f'line {magenta}{lno_foo+6}{reset}, in {magenta}test_colorized_traceback_is_the_default{reset}', | ||
f' {red}foo{reset+boldr}(){reset}', | ||
f' {red}~~~{reset+boldr}^^{reset}', | ||
f' File {magenta}"{__file__}"{reset}, ' | ||
f'line {magenta}{lno_foo+1}{reset}, in {magenta}foo{reset}', | ||
f' {red}1{reset+boldr}/{reset+red}0{reset}', | ||
f' {red}~{reset+boldr}^{reset+red}~{reset}', | ||
f'{boldm}ZeroDivisionError{reset}: {magenta}division by zero{reset}', | ||
] | ||
self.assertEqual(actual, expected) | ||
|
||
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() | ||
with virtual_patching: | ||
with unittest.mock.patch("os.isatty&q 8000 uot;) as isatty_mock: | ||
isatty_mock.return_value = True | ||
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}): | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}): | ||
self.assertEqual(_colorize.can_colorize(), True) | ||
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}): | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}): | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PYTHON_COLORS": '1'}): | ||
self.assertEqual(_colorize.can_colorize(), True) | ||
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}): | ||
self.assertEqual(_colorize.can_colorize(), True) | ||
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}): | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}): | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
isatty_mock.return_value = False | ||
self.assertEqual(_colorize.can_colorize(), False) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.