Skip to content

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 27 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
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 Apr 6, 2024
d27c0a8
Use _colorize in traceback module
hugovk Apr 6, 2024
bb591b6
Fix whitespace
hugovk Apr 6, 2024
42079be
Use f-strings
hugovk Apr 6, 2024
0088579
Remove underscores from members of an underscored module
hugovk Apr 6, 2024
d3034fa
Add blurb
hugovk Apr 6, 2024
39780cb
Remove underscores from members of an underscored module
hugovk Apr 6, 2024
c5aec15
Revert "Fix whitespace"
hugovk Apr 6, 2024
7e40133
Move _colorize to stdlib block, colour->color
hugovk Apr 6, 2024
e484465
Move imports together
hugovk Apr 6, 2024
1c7b025
Move imports together
hugovk Apr 6, 2024
ab2c94c
Move imports together
hugovk Apr 6, 2024
1aaeab8
Revert notests -> no_tests
hugovk Apr 6, 2024
cd02e4a
Revert "Use f-strings"
hugovk Apr 6, 2024
06543ff
Fix local tests
hugovk Apr 6, 2024
31c6647
Use red divider for failed test
hugovk Apr 7, 2024
9be3d81
Fix local tests
hugovk Apr 7, 2024
e4ff3e3
Less red
hugovk Apr 7, 2024
b62500a
Revert unnecessary changes
hugovk Apr 7, 2024
eb4f8dc
Move colour tests to test__colorize.py
hugovk Apr 7, 2024
976bfb4
Refactor asserts
hugovk Apr 7, 2024
ad7a946
Add missing captured_output to test.support's __all__ to fix IDE warning
hugovk Apr 7, 2024
796e9f2
Only move test_colorized_detection_checks_for_environment_variables f…
hugovk Apr 7, 2024
99d4d0c
Apply suggestions from code review
hugovk Apr 7, 2024
95b9831
Use unittest's enterContext
hugovk Apr 7, 2024
d5417b4
Merge remote-tracking branch 'upstream/main' into doctest-tidy-output…
hugovk Apr 16, 2024
ece3ce0
Keep colorize functionality in traceback module for now
hugovk Apr 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Keep colorize functionality in traceback module for now
  • Loading branch information
hugovk committed Apr 17, 2024
commit ece3ce0f70d5a44e682d1545dd7d5146cba2d39a
45 changes: 0 additions & 45 deletions Lib/_colorize.py

This file was deleted.

25 changes: 12 additions & 13 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def _test():
import unittest
from io import StringIO, IncrementalNewlineDecoder
from collections import namedtuple
import _colorize # Used in doctests
from _colorize import ANSIColors, can_colorize
from traceback import _ANSIColors, _can_colorize


class TestResults(namedtuple('TestResults', 'failed attempted')):
Expand Down Expand Up @@ -1181,8 +1180,8 @@ class DocTestRunner:
The `run` method is used to process a single DocTest case. It
returns a TestResults instance.

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> tests = DocTestFinder().find(_TestClass)
>>> runner = DocTestRunner(verbose=False)
Expand Down Expand Up @@ -1235,7 +1234,7 @@ class DocTestRunner:
overriding the methods `report_start`, `report_success`,
`report_unexpected_exception`, and `report_failure`.

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""
# This divider string is used to separate failure messages, and to
# separate sections of the summary.
Expand Down Expand Up @@ -1315,7 +1314,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):

def _failure_header(self, test, example):
red, reset = (
(ANSIColors.RED, ANSIColors.RESET) if can_colorize() else ("", "")
(_ANSIColors.RED, _ANSIColors.RESET) if _can_colorize() else ("", "")
)
out = [f"{red}{self.DIVIDER}{reset}"]
if test.filename:
Expand Down Expand Up @@ -1602,13 +1601,13 @@ def summarize(self, verbose=None):
else:
failed.append((name, (failures, tries, skips)))

if can_colorize():
bold_green = ANSIColors.BOLD_GREEN
bold_red = ANSIColors.BOLD_RED
green = ANSIColors.GREEN
red = ANSIColors.RED
reset = ANSIColors.RESET
yellow = ANSIColors.YELLOW
if _can_colorize():
bold_green = _ANSIColors.BOLD_GREEN
bold_red = _ANSIColors.BOLD_RED
green = _ANSIColors.GREEN
red = _ANSIColors.RED
reset = _ANSIColors.RESET
yellow = _ANSIColors.YELLOW
else:
bold_green = ""
bold_red = ""
Expand Down
40 changes: 0 additions & 40 deletions Lib/test/test__colorize.py

This file was deleted.

58 changes: 29 additions & 29 deletions Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import tempfile
import types
import contextlib
import _colorize # used in doctests
import traceback


def doctest_skip_if(condition):
Expand Down Expand Up @@ -471,7 +471,7 @@ def basics(): r"""
>>> tests = finder.find(sample_func)

>>> print(tests) # doctest: +ELLIPSIS
[<DocTest sample_func from test_doctest.py:37 (1 example)>]
[<DocTest sample_func from test_doctest.py:38 (1 example)>]

The exact name depends on how test_doctest was invoked, so allow for
leading path components.
Expand Down Expand Up @@ -893,8 +893,8 @@ def basics(): r"""
DocTestRunner is used to run DocTest test cases, and to accumulate
statistics. Here's a simple DocTest case we can use:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> def f(x):
... '''
Expand Down Expand Up @@ -951,7 +951,7 @@ def basics(): r"""
ok
TestResults(failed=1, attempted=3)

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""
def verbose_flag(): r"""
The `verbose` flag makes the test runner generate more detailed
Expand Down Expand Up @@ -1027,8 +1027,8 @@ def exceptions(): r"""
lines between the first line and the type/value may be omitted or
replaced with any other string:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> def f(x):
... '''
Expand Down Expand Up @@ -1261,7 +1261,7 @@ def exceptions(): r"""
ZeroDivisionError: integer division or modulo by zero
TestResults(failed=1, attempted=1)

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""
def displayhook(): r"""
Test that changing sys.displayhook doesn't matter for doctest.
Expand Down Expand Up @@ -1303,8 +1303,8 @@ def optionflags(): r"""
The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
and 1/0:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> def f(x):
... '>>> True\n1\n'
Expand Down Expand Up @@ -1725,7 +1725,7 @@ def optionflags(): r"""

Clean up.
>>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize

"""

Expand All @@ -1736,8 +1736,8 @@ def option_directives(): r"""
single example. To turn an option on for an example, follow that
example with a comment of the form ``# doctest: +OPTION``:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> def f(x): r'''
... >>> print(list(range(10))) # should fail: no ellipsis
Expand Down Expand Up @@ -1947,7 +1947,7 @@ def option_directives(): r"""
Traceback (most recent call last):
ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""

def test_testsource(): r"""
Expand Down Expand Up @@ -2031,8 +2031,8 @@ def test_pdb_set_trace():
with a version that restores stdout. This is necessary for you to
see debugger output.

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> doc = '''
... >>> x = 42
Expand Down Expand Up @@ -2157,7 +2157,7 @@ def test_pdb_set_trace():
9
TestResults(failed=1, attempted=3)

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""

def test_pdb_set_trace_nested():
Expand Down Expand Up @@ -2679,8 +2679,8 @@ def test_testfile(): r"""

We don't want color or `-v` in sys.argv for these tests.

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> save_argv = sys.argv
>>> if '-v' in sys.argv:
Expand Down Expand Up @@ -2848,7 +2848,7 @@ def test_testfile(): r"""
TestResults(failed=0, attempted=2)
>>> doctest.master = None # Reset master.
>>> sys.argv = save_argv
>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""

class TestImporter(importlib.abc.MetaPathFinder, importlib.abc.ResourceLoader):
Expand Down Expand Up @@ -2986,8 +2986,8 @@ def test_testmod(): r"""
def test_unicode(): """
Check doctest with a non-ascii filename:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> doc = '''
... >>> raise Exception('clé')
Expand Down Expand Up @@ -3015,7 +3015,7 @@ def test_unicode(): """
Exception: clé
TestResults(failed=1, attempted=1)

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""


Expand Down Expand Up @@ -3310,8 +3310,8 @@ def test_run_doctestsuite_multiple_times():

def test_exception_with_note(note):
"""
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> test_exception_with_note('Note')
Traceback (most recent call last):
Expand Down Expand Up @@ -3363,7 +3363,7 @@ def test_exception_with_note(note):
note
TestResults(failed=1, attempted=...)

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""
exc = ValueError('Text')
exc.add_note(note)
Expand Down Expand Up @@ -3444,8 +3444,8 @@ def test_syntax_error_subclass_from_stdlib():

def test_syntax_error_with_incorrect_expected_note():
"""
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False

>>> def f(x):
... r'''
Expand Down Expand Up @@ -3476,7 +3476,7 @@ def test_syntax_error_with_incorrect_expected_note():
note2
TestResults(failed=1, attempted=...)

>>> _colorize.COLORIZE = save_colorize
>>> traceback._COLORIZE = save_colorize
"""


Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy