1616import tempfile
1717import types
1818import contextlib
19+ import traceback
1920
2021
2122def doctest_skip_if (condition ):
@@ -470,7 +471,7 @@ def basics(): r"""
470471 >>> tests = finder.find(sample_func)
471472
472473 >>> print(tests) # doctest: +ELLIPSIS
473- [<DocTest sample_func from test_doctest.py:37 (1 example)>]
474+ [<DocTest sample_func from test_doctest.py:38 (1 example)>]
474475
475476The exact name depends on how test_doctest was invoked, so allow for
476477leading path components.
@@ -892,6 +893,9 @@ def basics(): r"""
892893DocTestRunner is used to run DocTest test cases, and to accumulate
893894statistics. Here's a simple DocTest case we can use:
894895
896+ >>> save_colorize = traceback._COLORIZE
897+ >>> traceback._COLORIZE = False
898+
895899 >>> def f(x):
896900 ... '''
897901 ... >>> x = 12
@@ -946,6 +950,8 @@ def basics(): r"""
946950 6
947951 ok
948952 TestResults(failed=1, attempted=3)
953+
954+ >>> traceback._COLORIZE = save_colorize
949955"""
950956 def verbose_flag (): r"""
951957The `verbose` flag makes the test runner generate more detailed
@@ -1021,6 +1027,9 @@ def exceptions(): r"""
10211027lines between the first line and the type/value may be omitted or
10221028replaced with any other string:
10231029
1030+ >>> save_colorize = traceback._COLORIZE
1031+ >>> traceback._COLORIZE = False
1032+
10241033 >>> def f(x):
10251034 ... '''
10261035 ... >>> x = 12
@@ -1251,6 +1260,8 @@ def exceptions(): r"""
12511260 ...
12521261 ZeroDivisionError: integer division or modulo by zero
12531262 TestResults(failed=1, attempted=1)
1263+
1264+ >>> traceback._COLORIZE = save_colorize
12541265"""
12551266 def displayhook (): r"""
12561267Test that changing sys.displayhook doesn't matter for doctest.
@@ -1292,6 +1303,9 @@ def optionflags(): r"""
12921303The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
12931304and 1/0:
12941305
1306+ >>> save_colorize = traceback._COLORIZE
1307+ >>> traceback._COLORIZE = False
1308+
12951309 >>> def f(x):
12961310 ... '>>> True\n1\n'
12971311
@@ -1711,6 +1725,7 @@ def optionflags(): r"""
17111725
17121726Clean up.
17131727 >>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
1728+ >>> traceback._COLORIZE = save_colorize
17141729
17151730 """
17161731
@@ -1721,6 +1736,9 @@ def option_directives(): r"""
17211736single example. To turn an option on for an example, follow that
17221737example with a comment of the form ``# doctest: +OPTION``:
17231738
1739+ >>> save_colorize = traceback._COLORIZE
1740+ >>> traceback._COLORIZE = False
1741+
17241742 >>> def f(x): r'''
17251743 ... >>> print(list(range(10))) # should fail: no ellipsis
17261744 ... [0, 1, ..., 9]
@@ -1928,6 +1946,8 @@ def option_directives(): r"""
19281946 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
19291947 Traceback (most recent call last):
19301948 ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
1949+
1950+ >>> traceback._COLORIZE = save_colorize
19311951"""
19321952
19331953def test_testsource (): r"""
@@ -2011,6 +2031,9 @@ def test_pdb_set_trace():
20112031 with a version that restores stdout. This is necessary for you to
20122032 see debugger output.
20132033
2034+ >>> save_colorize = traceback._COLORIZE
2035+ >>> traceback._COLORIZE = False
2036+
20142037 >>> doc = '''
20152038 ... >>> x = 42
20162039 ... >>> raise Exception('clé')
@@ -2065,7 +2088,7 @@ def test_pdb_set_trace():
20652088 ... finally:
20662089 ... sys.stdin = real_stdin
20672090 --Return--
2068- > <doctest test.test_doctest.test_doctest.test_pdb_set_trace[7 ]>(3)calls_set_trace()->None
2091+ > <doctest test.test_doctest.test_doctest.test_pdb_set_trace[9 ]>(3)calls_set_trace()->None
20692092 -> import pdb; pdb.set_trace()
20702093 (Pdb) print(y)
20712094 2
@@ -2133,6 +2156,8 @@ def test_pdb_set_trace():
21332156 Got:
21342157 9
21352158 TestResults(failed=1, attempted=3)
2159+
2160+ >>> traceback._COLORIZE = save_colorize
21362161 """
21372162
21382163 def test_pdb_set_trace_nested ():
@@ -2667,7 +2692,10 @@ def test_testfile(): r"""
26672692called with the name of a file, which is taken to be relative to the
26682693calling module. The return value is (#failures, #tests).
26692694
2670- We don't want `-v` in sys.argv for these tests.
2695+ We don't want color or `-v` in sys.argv for these tests.
2696+
2697+ >>> save_colorize = traceback._COLORIZE
2698+ >>> traceback._COLORIZE = False
26712699
26722700 >>> save_argv = sys.argv
26732701 >>> if '-v' in sys.argv:
@@ -2835,6 +2863,7 @@ def test_testfile(): r"""
28352863 TestResults(failed=0, attempted=2)
28362864 >>> doctest.master = None # Reset master.
28372865 >>> sys.argv = save_argv
2866+ >>> traceback._COLORIZE = save_colorize
28382867"""
28392868
28402869class TestImporter (importlib .abc .MetaPathFinder , importlib .abc .ResourceLoader ):
@@ -2972,6 +3001,9 @@ def test_testmod(): r"""
29723001 def test_unicode (): """
29733002Check doctest with a non-ascii filename:
29743003
3004+ >>> save_colorize = traceback._COLORIZE
3005+ >>> traceback._COLORIZE = False
3006+
29753007 >>> doc = '''
29763008 ... >>> raise Exception('clé')
29773009 ... '''
@@ -2997,8 +3029,11 @@ def test_unicode(): """
29973029 raise Exception('clé')
29983030 Exception: clé
29993031 TestResults(failed=1, attempted=1)
3032+
3033+ >>> traceback._COLORIZE = save_colorize
30003034 """
30013035
3036+
30023037@doctest_skip_if (not support .has_subprocess_support )
30033038def test_CLI (): r"""
30043039The doctest module can be used to run doctests against an arbitrary file.
@@ -3290,6 +3325,9 @@ def test_run_doctestsuite_multiple_times():
32903325
32913326def test_exception_with_note (note ):
32923327 """
3328+ >>> save_colorize = traceback._COLORIZE
3329+ >>> traceback._COLORIZE = False
3330+
32933331 >>> test_exception_with_note('Note')
32943332 Traceback (most recent call last):
32953333 ...
@@ -3339,6 +3377,8 @@ def test_exception_with_note(note):
33393377 ValueError: message
33403378 note
33413379 TestResults(failed=1, attempted=...)
3380+
3381+ >>> traceback._COLORIZE = save_colorize
33423382 """
33433383 exc = ValueError ('Text' )
33443384 exc .add_note (note )
@@ -3419,6 +3459,9 @@ def test_syntax_error_subclass_from_stdlib():
34193459
34203460def test_syntax_error_with_incorrect_expected_note ():
34213461 """
3462+ >>> save_colorize = traceback._COLORIZE
3463+ >>> traceback._COLORIZE = False
3464+
34223465 >>> def f(x):
34233466 ... r'''
34243467 ... >>> exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
@@ -3447,6 +3490,8 @@ def test_syntax_error_with_incorrect_expected_note():
34473490 note1
34483491 note2
34493492 TestResults(failed=1, attempted=...)
3493+
3494+ >>> traceback._COLORIZE = save_colorize
34503495 """
34513496
34523497
0 commit comments