21
21
from test .support .os_helper import TESTFN , unlink
22
22
from test .support .script_helper import assert_python_ok , assert_python_failure
23
23
from test .support .import_helper import forget
24
+ from test .support import force_not_colorized
24
25
25
26
import json
26
27
import textwrap
39
40
40
41
LEVENSHTEIN_DATA_FILE = Path (__file__ ).parent / 'levenshtein_examples.json'
41
42
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
42
50
43
51
class TracebackCases (unittest .TestCase ):
44
52
# For now, a very minimal set of tests. I want to be sure that
@@ -124,6 +132,7 @@ def test_nocaret(self):
124
132
self .assertEqual (len (err ), 3 )
125
133
self .assertEqual (err [1 ].strip (), "bad syntax" )
126
134
135
+ @force_not_colorized
127
136
def test_no_caret_with_no_debug_ranges_flag (self ):
128
137
# Make sure that if `-X no_debug_ranges` is used, there are no carets
129
138
# in the traceback.
@@ -401,7 +410,7 @@ def do_test(firstlines, message, charset, lineno):
401
410
""" .format (firstlines , message ))
402
411
403
412
process = subprocess .Popen ([sys .executable , TESTFN ],
404
- stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
413
+ stdout = subprocess .PIPE , stderr = subprocess .STDOUT , env = {} )
405
414
stdout , stderr = process .communicate ()
406
415
stdout = stdout .decode (output_encoding ).splitlines ()
407
416
finally :
@@ -4354,13 +4363,18 @@ def foo():
4354
4363
f'{ boldm } ZeroDivisionError{ reset } : { magenta } division by zero{ reset } ' ]
4355
4364
self .assertEqual (actual , expected )
4356
4365
4366
+ @force_not_colorized
4357
4367
def test_colorized_detection_checks_for_environment_variables (self ):
4358
4368
if sys .platform == "win32" :
4359
4369
virtual_patching = unittest .mock .patch ("nt._supports_virtual_terminal" , return_value = True )
4360
4370
else :
4361
4371
virtual_patching = contextlib .nullcontext ()
4362
4372
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 )):
4364
4378
isatty_mock .return_value = True
4365
4379
with unittest .mock .patch ("os.environ" , {'TERM' : 'dumb' }):
4366
4380
self .assertEqual (traceback ._can_colorize (), False )
@@ -4379,7 +4393,8 @@ def test_colorized_detection_checks_for_environment_variables(self):
4379
4393
with unittest .mock .patch ("os.environ" , {'FORCE_COLOR' : '1' , "PYTHON_COLORS" : '0' }):
4380
4394
self .assertEqual (traceback ._can_colorize (), False )
4381
4395
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 )
4383
4398
4384
4399
if __name__ == "__main__" :
4385
4400
unittest .main ()
0 commit comments