@@ -104,7 +104,8 @@ def _test():
104104import unittest
105105from io import StringIO , IncrementalNewlineDecoder
106106from collections import namedtuple
107- from traceback import _ANSIColors , _can_colorize
107+ import _colorize # Used in doctests
108+ from _colorize import ANSIColors , can_colorize
108109
109110
110111class TestResults (namedtuple ('TestResults' , 'failed attempted' )):
@@ -1180,8 +1181,8 @@ class DocTestRunner:
11801181 The `run` method is used to process a single DocTest case. It
11811182 returns a TestResults instance.
11821183
1183- >>> save_colorize = traceback._COLORIZE
1184- >>> traceback._COLORIZE = False
1184+ >>> save_colorize = _colorize.COLORIZE
1185+ >>> _colorize.COLORIZE = False
11851186
11861187 >>> tests = DocTestFinder().find(_TestClass)
11871188 >>> runner = DocTestRunner(verbose=False)
@@ -1234,7 +1235,7 @@ class DocTestRunner:
12341235 overriding the methods `report_start`, `report_success`,
12351236 `report_unexpected_exception`, and `report_failure`.
12361237
1237- >>> traceback._COLORIZE = save_colorize
1238+ >>> _colorize.COLORIZE = save_colorize
12381239 """
12391240 # This divider string is used to separate failure messages, and to
12401241 # separate sections of the summary.
@@ -1314,7 +1315,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):
13141315
13151316 def _failure_header (self , test , example ):
13161317 red , reset = (
1317- (_ANSIColors .RED , _ANSIColors .RESET ) if _can_colorize () else ("" , "" )
1318+ (ANSIColors .RED , ANSIColors .RESET ) if can_colorize () else ("" , "" )
13181319 )
13191320 out = [f"{ red } { self .DIVIDER } { reset } " ]
13201321 if test .filename :
@@ -1556,8 +1557,8 @@ def out(s):
15561557 # Make sure sys.displayhook just prints the value to stdout
15571558 save_displayhook = sys .displayhook
15581559 sys .displayhook = sys .__displayhook__
1559- saved_can_colorize = traceback . _can_colorize
1560- traceback . _can_colorize = lambda : False
1560+ saved_can_colorize = _colorize . can_colorize
1561+ _colorize . can_colorize = lambda : False
15611562 color_variables = {"PYTHON_COLORS" : None , "FORCE_COLOR" : None }
15621563 for key in color_variables :
15631564 color_variables [key ] = os .environ .pop (key , None )
@@ -1569,7 +1570,7 @@ def out(s):
15691570 sys .settrace (save_trace )
15701571 linecache .getlines = self .save_linecache_getlines
15711572 sys .displayhook = save_displayhook
1572- traceback . _can_colorize = saved_can_colorize
1573+ _colorize . can_colorize = saved_can_colorize
15731574 for key , value in color_variables .items ():
15741575 if value is not None :
15751576 os .environ [key ] = value
@@ -1609,20 +1610,13 @@ def summarize(self, verbose=None):
16091610 else :
16101611 failed .append ((name , (failures , tries , skips )))
16111612
1612- if _can_colorize ():
1613- bold_green = _ANSIColors .BOLD_GREEN
1614- bold_red = _ANSIColors .BOLD_RED
1615- green = _ANSIColors .GREEN
1616- red = _ANSIColors .RED
1617- reset = _ANSIColors .RESET
1618- yellow = _ANSIColors .YELLOW
1619- else :
1620- bold_green = ""
1621- bold_red = ""
1622- green = ""
1623- red = ""
1624- reset = ""
1625- yellow = ""
1613+ ansi = _colorize .get_colors ()
1614+ bold_green = ansi .BOLD_GREEN
1615+ bold_red = ansi .BOLD_RED
1616+ green = ansi .GREEN
1617+ red = ansi .RED
1618+ reset = ansi .RESET
1619+ yellow = ansi .YELLOW
16261620
16271621 if verbose :
16281622 if notests :
0 commit comments