Skip to content

Commit cc3dc8a

Browse files
authored
[3.13] gh-127873: Only check sys.flags.ignore_environment for PYTHON* env vars (GH-127877) (#129138)
1 parent f7f8b8b commit cc3dc8a

22 files changed

+91
-28
lines changed

.github/CODEOWNERS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,7 @@ Lib/test/test_interpreters/ @ericsnowcurrently
267267
# Config Parser
268268
Lib/configparser.py @jaraco
269269
Lib/test/test_configparser.py @jaraco
270+
271+
# Colorize
272+
Lib/_colorize.py @hugovk
273+
Lib/test/test__colorize.py @hugovk

Lib/_colorize.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ def can_colorize(*, file=None) -> bool:
4040
return False
4141
if os.environ.get("PYTHON_COLORS") == "1":
4242
return True
43-
if "NO_COLOR" in os.environ:
44-
return False
43+
if "NO_COLOR" in os.environ:
44+
return False
4545
if not COLORIZE:
4646
return False
47-
if not sys.flags.ignore_environment:
48-
if "FORCE_COLOR" in os.environ:
49-
return True
50-
if os.environ.get("TERM") == "dumb":
51-
return False
47+
if "FORCE_COLOR" in os.environ:
48+
return True
49+
if os.environ.get("TERM") == "dumb":
50+
return False
5251

5352
if not hasattr(file, "fileno"):
5453
return False

Lib/test/support/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"without_optimizer",
6262
"force_not_colorized",
6363
"force_not_colorized_test_class",
64+
"make_clean_env",
6465
"BrokenIter",
6566
]
6667

@@ -2732,6 +2733,16 @@ def new_setUpClass(cls):
27322733
return cls
27332734

27342735

2736+
def make_clean_env() -> dict[str, str]:
2737+
clean_env = os.environ.copy()
2738+
for k in clean_env.copy():
2739+
if k.startswith("PYTHON"):
2740+
clean_env.pop(k)
2741+
clean_env.pop("FORCE_COLOR", None)
2742+
clean_env.pop("NO_COLOR", None)
2743+
return clean_env
2744+
2745+
27352746
def initialized_with_pyrepl():
27362747
"""Detect whether PyREPL was used during Python initialization."""
27372748
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.

Lib/test/test__colorize.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
import unittest.mock
55
import _colorize
6-
from test.support import force_not_colorized
6+
from test.support import force_not_colorized, make_clean_env
77

88
ORIGINAL_CAN_COLORIZE = _colorize.can_colorize
99

@@ -17,6 +17,14 @@ def tearDownModule():
1717

1818

1919
class TestColorizeFunction(unittest.TestCase):
20+
def setUp(self):
21+
# Remove PYTHON* environment variables to isolate from local user
22+
# settings and simulate running with `-E`. Such variables should be
23+
# added to test methods later to patched os.environ.
24+
patcher = unittest.mock.patch("os.environ", new=make_clean_env())
25+
self.addCleanup(patcher.stop)
26+
patcher.start()
27+
2028
@force_not_colorized
2129
def test_colorized_detection_checks_for_environment_variables(self):
2230
flags = unittest.mock.MagicMock(ignore_environment=False)

Lib/test/test_capi/test_misc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class InstanceMethod:
7575
id = _testcapi.instancemethod(id)
7676
testfunction = _testcapi.instancemethod(testfunction)
7777

78+
79+
@support.force_not_colorized_test_class
7880
class CAPITest(unittest.TestCase):
7981

8082
def test_instancemethod(self):

Lib/test/test_cmd_line_script.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
8888
importlib.invalidate_caches()
8989
return to_return
9090

91+
92+
@support.force_not_colorized_test_class
9193
class CmdLineTest(unittest.TestCase):
9294
def _check_output(self, script_name, exit_code, data,
9395
expected_file, expected_argv0,

Lib/test/test_compileall.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ def test_d_compile_error(self):
766766
rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
767767
self.assertRegex(out, b'File "dinsdale')
768768

769+
@support.force_not_colorized
769770
def test_d_runtime_error(self):
770771
bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
771772
self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)

Lib/test/test_eof.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44
from codecs import BOM_UTF8
5-
from test import support
5+
from test.support import force_not_colorized
66
from test.support import os_helper
77
from test.support import script_helper
88
from test.support import warnings_helper
@@ -44,6 +44,7 @@ def test_EOFS(self):
4444
self.assertEqual(cm.exception.text, "ä = '''thîs is ")
4545
self.assertEqual(cm.exception.offset, 5)
4646

47+
@force_not_colorized
4748
def test_EOFS_with_file(self):
4849
expect = ("(<string>, line 1)")
4950
with os_helper.temp_dir() as temp_dir:
@@ -123,6 +124,7 @@ def test_line_continuation_EOF(self):
123124
self.assertEqual(str(cm.exception), expect)
124125

125126
@unittest.skipIf(not sys.executable, "sys.executable required")
127+
@force_not_colorized
126128
def test_line_continuation_EOF_from_file_bpo2180(self):
127129
"""Ensure tok_nextc() does not add too many ending newlines."""
128130
with os_helper.temp_dir() as temp_dir:

Lib/test/test_exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,7 @@ def gen():
14651465

14661466
@cpython_only
14671467
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1468+
@force_not_colorized
14681469
def test_recursion_normalizing_infinite_exception(self):
14691470
# Issue #30697. Test that a RecursionError is raised when
14701471
# maximum recursion depth has been exceeded when creating
@@ -2157,6 +2158,7 @@ def test_multiline_not_highlighted(self):
21572158
self.assertEqual(result[-len(expected):], expected)
21582159

21592160

2161+
@support.force_not_colorized_test_class
21602162
class SyntaxErrorTests(unittest.TestCase):
21612163
maxDiff = None
21622164

Lib/test/test_import/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,20 @@
2929

3030
from test.support import os_helper
3131
from test.support import (
32-
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_apple_mobile, is_emscripten,
33-
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS,
34-
requires_gil_enabled, Py_GIL_DISABLED)
32+
STDLIB_DIR,
33+
swap_attr,
34+
swap_item,
35+
cpython_only,
36+
is_apple_mobile,
37+
is_emscripten,
38+
is_wasi,
39+
run_in_subinterp,
40+
run_in_subinterp_with_config,
41+
Py_TRACE_REFS,
42+
requires_gil_enabled,
43+
Py_GIL_DISABLED,
44+
force_not_colorized_test_class,
45+
)
3546
from test.support.import_helper import (
3647
forget, make_legacy_pyc, unlink, unload, ready_to_import,
3748
DirsOnSysPath, CleanImport, import_module)
@@ -352,6 +363,7 @@ def _from_subinterp(cls, name, interpid, pipe, script_kwargs):
352363
return cls.parse(text.decode())
353364

354365

366+
@force_not_colorized_test_class
355367
class ImportTests(unittest.TestCase):
356368

357369
def setUp(self):

0 commit comments

Comments
 (0)