Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 85c2037

Browse filesBrowse files
picnixzhugovk
andauthored
[3.13] gh-131277: allow EnvironmentVarGuard to unset more than one environment variable at once (GH-131280) (#131409)
(cherry picked from commit 3185e31) --------- Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 25ae204 commit 85c2037
Copy full SHA for 85c2037

10 files changed

+27
-36
lines changed

‎Lib/test/support/__init__.py

Copy file name to clipboardExpand all lines: Lib/test/support/__init__.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,8 +2707,7 @@ def no_color():
27072707
swap_attr(_colorize, "can_colorize", lambda file=None: False),
27082708
EnvironmentVarGuard() as env,
27092709
):
2710-
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
2711-
env.unset(var)
2710+
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")
27122711
env.set("NO_COLOR", "1")
27132712
yield
27142713

‎Lib/test/support/os_helper.py

Copy file name to clipboardExpand all lines: Lib/test/support/os_helper.py
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,10 @@ def temp_umask(umask):
693693

694694

695695
class EnvironmentVarGuard(collections.abc.MutableMapping):
696+
"""Class to help protect the environment variable properly.
696697
697-
"""Class to help protect the environment variable properly. Can be used as
698-
a context manager."""
698+
Can be used as a context manager.
699+
"""
699700

700701
def __init__(self):
701702
self._environ = os.environ
@@ -729,8 +730,10 @@ def __len__(self):
729730
def set(self, envvar, value):
730731
self[envvar] = value
731732

732-
def unset(self, envvar):
733-
del self[envvar]
733+
def unset(self, envvar, /, *envvars):
734+
"""Unset one or more environment variables."""
735+
for ev in (envvar, *envvars):
736+
del self[ev]
734737

735738
def copy(self):
736739
# We do what os.environ.copy() does.

‎Lib/test/test__colorize.py

Copy file name to clipboardExpand all lines: Lib/test/test__colorize.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
@contextlib.contextmanager
1111
def clear_env():
1212
with EnvironmentVarGuard() as mock_env:
13-
for var in "FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS", "TERM":
14-
mock_env.unset(var)
13+
mock_env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS", "TERM")
1514
yield mock_env
1615

1716

‎Lib/test/test__osx_support.py

Copy file name to clipboardExpand all lines: Lib/test/test__osx_support.py
+7-6Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ def setUp(self):
2020
self.prog_name = 'bogus_program_xxxx'
2121
self.temp_path_dir = os.path.abspath(os.getcwd())
2222
self.env = self.enterContext(os_helper.EnvironmentVarGuard())
23-
for cv in ('CFLAGS', 'LDFLAGS', 'CPPFLAGS',
24-
'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC',
25-
'CXX', 'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS',
26-
'PY_CORE_CFLAGS', 'PY_CORE_LDFLAGS'):
27-
if cv in self.env:
28-
self.env.unset(cv)
23+
24+
self.env.unset(
25+
'CFLAGS', 'LDFLAGS', 'CPPFLAGS',
26+
'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC',
27+
'CXX', 'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS',
28+
'PY_CORE_CFLAGS', 'PY_CORE_LDFLAGS'
29+
)
2930

3031
def add_expected_saved_initial_values(self, config_vars, expected_vars):
3132
# Ensure that the initial values for all modified config vars

‎Lib/test/test_getopt.py

Copy file name to clipboardExpand all lines: Lib/test/test_getopt.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
class GetoptTests(unittest.TestCase):
1414
def setUp(self):
1515
self.env = self.enterContext(EnvironmentVarGuard())
16-
if "POSIXLY_CORRECT" in self.env:
17-
del self.env["POSIXLY_CORRECT"]
16+
del self.env["POSIXLY_CORRECT"]
1817

1918
def assertError(self, *args, **kwargs):
2019
self.assertRaises(getopt.GetoptError, *args, **kwargs)

‎Lib/test/test_pathlib/test_pathlib.py

Copy file name to clipboardExpand all lines: Lib/test/test_pathlib/test_pathlib.py
+3-7Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ def test_expanduser_posix(self):
13911391
p7 = P(f'~{fakename}/Documents')
13921392

13931393
with os_helper.EnvironmentVarGuard() as env:
1394-
env.pop('HOME', None)
1394+
env.unset('HOME')
13951395

13961396
self.assertEqual(p1.expanduser(), P(userhome) / 'Documents')
13971397
self.assertEqual(p2.expanduser(), P(userhome) / 'Documents')
@@ -1504,10 +1504,7 @@ def test_absolute_windows(self):
15041504
def test_expanduser_windows(self):
15051505
P = self.cls
15061506
with os_helper.EnvironmentVarGuard() as env:
1507-
env.pop('HOME', None)
1508-
env.pop('USERPROFILE', None)
1509-
env.pop('HOMEPATH', None)
1510-
env.pop('HOMEDRIVE', None)
1507+
env.unset('HOME', 'USERPROFILE', 'HOMEPATH', 'HOMEDRIVE')
15111508
env['USERNAME'] = 'alice'
15121509

15131510
# test that the path returns unchanged
@@ -1545,8 +1542,7 @@ def check():
15451542
env['HOMEPATH'] = 'Users\\alice'
15461543
check()
15471544

1548-
env.pop('HOMEDRIVE', None)
1549-
env.pop('HOMEPATH', None)
1545+
env.unset('HOMEDRIVE', 'HOMEPATH')
15501546
env['USERPROFILE'] = 'C:\\Users\\alice'
15511547
check()
15521548

‎Lib/test/test_platform.py

Copy file name to clipboardExpand all lines: Lib/test/test_platform.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ def raises_oserror(*a):
336336
with support.swap_attr(platform, '_wmi_query', raises_oserror):
337337
with os_helper.EnvironmentVarGuard() as environ:
338338
try:
339-
if 'PROCESSOR_ARCHITEW6432' in environ:
340-
del environ['PROCESSOR_ARCHITEW6432']
339+
del environ['PROCESSOR_ARCHITEW6432']
341340
environ['PROCESSOR_ARCHITECTURE'] = 'foo'
342341
platform._uname_cache = None
343342
system, node, release, version, machine, processor = platform.uname()

‎Lib/test/test_regrtest.py

Copy file name to clipboardExpand all lines: Lib/test/test_regrtest.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ def create_regrtest(self, args):
419419
# which has an unclear API
420420
with os_helper.EnvironmentVarGuard() as env:
421421
# Ignore SOURCE_DATE_EPOCH env var if it's set
422-
if 'SOURCE_DATE_EPOCH' in env:
423-
del env['SOURCE_DATE_EPOCH']
422+
del env['SOURCE_DATE_EPOCH']
424423

425424
regrtest = main.Regrtest(ns)
426425

‎Lib/test/test_shutil.py

Copy file name to clipboardExpand all lines: Lib/test/test_shutil.py
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,7 +2465,7 @@ def test_environ_path_cwd(self):
24652465

24662466
def test_environ_path_missing(self):
24672467
with os_helper.EnvironmentVarGuard() as env:
2468-
env.pop('PATH', None)
2468+
del env['PATH']
24692469

24702470
# without confstr
24712471
with unittest.mock.patch('os.confstr', side_effect=ValueError, \
@@ -2491,7 +2491,7 @@ def test_empty_path(self):
24912491

24922492
def test_empty_path_no_PATH(self):
24932493
with os_helper.EnvironmentVarGuard() as env:
2494-
env.pop('PATH', None)
2494+
del env['PATH']
24952495
rv = shutil.which(self.file)
24962496
self.assertIsNone(rv)
24972497

@@ -3424,17 +3424,15 @@ def test_stty_match(self):
34243424
expected = (int(size[1]), int(size[0])) # reversed order
34253425

34263426
with os_helper.EnvironmentVarGuard() as env:
3427-
del env['LINES']
3428-
del env['COLUMNS']
3427+
env.unset('LINES', 'COLUMNS')
34293428
actual = shutil.get_terminal_size()
34303429

34313430
self.assertEqual(expected, actual)
34323431

34333432
@unittest.skipIf(support.is_wasi, "WASI has no /dev/null")
34343433
def test_fallback(self):
34353434
with os_helper.EnvironmentVarGuard() as env:
3436-
del env['LINES']
3437-
del env['COLUMNS']
3435+
env.unset('LINES', 'COLUMNS')
34383436

34393437
# sys.__stdout__ has no fileno()
34403438
with support.swap_attr(sys, '__stdout__', None):

‎Lib/test/test_site.py

Copy file name to clipboardExpand all lines: Lib/test/test_site.py
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,7 @@ def test_no_home_directory(self):
355355

356356
with EnvironmentVarGuard() as environ, \
357357
mock.patch('os.path.expanduser', lambda path: path):
358-
359-
del environ['PYTHONUSERBASE']
360-
del environ['APPDATA']
358+
environ.unset('PYTHONUSERBASE', 'APPDATA')
361359

362360
user_base = site.getuserbase()
363361
self.assertTrue(user_base.startswith('~' + os.sep),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.