2121from test .support .os_helper import TESTFN , unlink
2222from test .support .script_helper import assert_python_ok , assert_python_failure
2323from test .support .import_helper import forget
24+ from test .support import force_not_colorized
2425
2526import json
2627import textwrap
3940
4041LEVENSHTEIN_DATA_FILE = Path (__file__ ).parent / 'levenshtein_examples.json'
4142
43+ ORIGINAL_CAN_COLORIZE = traceback ._can_colorize
44+
45+ def setUpModule ():
46+ traceback ._can_colorize = lambda : False
47+
48+ def tearDownModule ():
49+ traceback ._can_colorize = ORIGINAL_CAN_COLORIZE
4250
4351class TracebackCases (unittest .TestCase ):
4452 # For now, a very minimal set of tests. I want to be sure that
@@ -124,6 +132,7 @@ def test_nocaret(self):
124132 self .assertEqual (len (err ), 3 )
125133 self .assertEqual (err [1 ].strip (), "bad syntax" )
126134
135+ @force_not_colorized
127136 def test_no_caret_with_no_debug_ranges_flag (self ):
128137 # Make sure that if `-X no_debug_ranges` is used, there are no carets
129138 # in the traceback.
@@ -401,7 +410,7 @@ def do_test(firstlines, message, charset, lineno):
401410 """ .format (firstlines , message ))
402411
403412 process = subprocess .Popen ([sys .executable , TESTFN ],
404- stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
413+ stdout = subprocess .PIPE , stderr = subprocess .STDOUT , env = {} )
405414 stdout , stderr = process .communicate ()
406415 stdout = stdout .decode (output_encoding ).splitlines ()
407416 finally :
@@ -4354,13 +4363,18 @@ def foo():
43544363 f'{ boldm } ZeroDivisionError{ reset } : { magenta } division by zero{ reset } ' ]
43554364 self .assertEqual (actual , expected )
43564365
4366+ @force_not_colorized
43574367 def test_colorized_detection_checks_for_environment_variables (self ):
43584368 if sys .platform == "win32" :
43594369 virtual_patching = unittest .mock .patch ("nt._supports_virtual_terminal" , return_value = True )
43604370 else :
43614371 virtual_patching = contextlib .nullcontext ()
43624372 with virtual_patching :
4363- with unittest .mock .patch ("os.isatty" ) as isatty_mock :
4373+
4374+ flags = unittest .mock .MagicMock (ignore_environment = False )
4375+ with (unittest .mock .patch ("os.isatty" ) as isatty_mock ,
4376+ unittest .mock .patch ("sys.flags" , flags ),
4377+ unittest .mock .patch ("traceback._can_colorize" , ORIGINAL_CAN_COLORIZE )):
43644378 isatty_mock .return_value = True
43654379 with unittest .mock .patch ("os.environ" , {'TERM' : 'dumb' }):
43664380 self .assertEqual (traceback ._can_colorize (), False )
@@ -4379,7 +4393,8 @@ def test_colorized_detection_checks_for_environment_variables(self):
43794393 with unittest .mock .patch ("os.environ" , {'FORCE_COLOR' : '1' , "PYTHON_COLORS" : '0' }):
43804394 self .assertEqual (traceback ._can_colorize (), False )
43814395 isatty_mock .return_value = False
4382- self .assertEqual (traceback ._can_colorize (), False )
4396+ with unittest .mock .patch ("os.environ" , {}):
4397+ self .assertEqual (traceback ._can_colorize (), False )
43834398
43844399if __name__ == "__main__" :
43854400 unittest .main ()
0 commit comments