Skip to content

Commit 3185e31

Browse files
picnixzhugovk
andauthored
gh-131277: allow EnvironmentVarGuard to unset more than one environment variable at once (#131280)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 9558d22 commit 3185e31

15 files changed

+38
-45
lines changed

Doc/library/test.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,9 +1435,12 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14351435
``value``.
14361436

14371437

1438-
.. method:: EnvironmentVarGuard.unset(envvar)
1438+
.. method:: EnvironmentVarGuard.unset(envvar, *others)
14391439

1440-
Temporarily unset the environment variable ``envvar``.
1440+
Temporarily unset one or more environment variables.
1441+
1442+
.. versionchanged:: next
1443+
More than one environment variable can be unset.
14411444

14421445

14431446
.. function:: can_symlink()

Lib/test/support/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,8 +2855,7 @@ def no_color():
28552855
swap_attr(_colorize, "can_colorize", lambda file=None: False),
28562856
EnvironmentVarGuard() as env,
28572857
):
2858-
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
2859-
env.unset(var)
2858+
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")
28602859
env.set("NO_COLOR", "1")
28612860
yield
28622861

Lib/test/support/os_helper.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,10 @@ def temp_umask(umask):
720720

721721

722722
class EnvironmentVarGuard(collections.abc.MutableMapping):
723+
"""Class to help protect the environment variable properly.
723724
724-
"""Class to help protect the environment variable properly. Can be used as
725-
a context manager."""
725+
Can be used as a context manager.
726+
"""
726727

727728
def __init__(self):
728729
self._environ = os.environ
@@ -756,8 +757,10 @@ def __len__(self):
756757
def set(self, envvar, value):
757758
self[envvar] = value
758759

759-
def unset(self, envvar):
760-
del self[envvar]
760+
def unset(self, envvar, /, *envvars):
761+
"""Unset one or more environment variables."""
762+
for ev in (envvar, *envvars):
763+
del self[ev]
761764

762765
def copy(self):
763766
# We do what os.environ.copy() does.

Lib/test/test__colorize.py

Lines 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

Lines 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_builtin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,7 @@ def test_open_default_encoding(self):
15681568
# try to get a user preferred encoding different than the current
15691569
# locale encoding to check that open() uses the current locale
15701570
# encoding and not the user preferred encoding
1571-
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
1572-
env.unset(key)
1571+
env.unset('LC_ALL', 'LANG', 'LC_CTYPE')
15731572

15741573
self.write_testfile()
15751574
current_locale_encoding = locale.getencoding()

Lib/test/test_getopt.py

Lines 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_io.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,8 +2896,7 @@ def test_default_encoding(self):
28962896
# try to get a user preferred encoding different than the current
28972897
# locale encoding to check that TextIOWrapper() uses the current
28982898
# locale encoding and not the user preferred encoding
2899-
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
2900-
env.unset(key)
2899+
env.unset('LC_ALL', 'LANG', 'LC_CTYPE')
29012900

29022901
current_locale_encoding = locale.getencoding()
29032902
b = self.BytesIO()

Lib/test/test_locale.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,7 @@ def test_defaults_UTF8(self):
500500

501501
try:
502502
with os_helper.EnvironmentVarGuard() as env:
503-
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
504-
env.unset(key)
505-
503+
env.unset('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')
506504
env.set('LC_CTYPE', 'UTF-8')
507505

508506
with check_warnings(('', DeprecationWarning)):

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,7 @@ def test_expanduser_posix(self):
32323232
p7 = P(f'~{fakename}/Documents')
32333233

32343234
with os_helper.EnvironmentVarGuard() as env:
3235-
env.pop('HOME', None)
3235+
env.unset('HOME')
32363236

32373237
self.assertEqual(p1.expanduser(), P(userhome) / 'Documents')
32383238
self.assertEqual(p2.expanduser(), P(userhome) / 'Documents')
@@ -3345,10 +3345,7 @@ def test_absolute_windows(self):
33453345
def test_expanduser_windows(self):
33463346
P = self.cls
33473347
with os_helper.EnvironmentVarGuard() as env:
3348-
env.pop('HOME', None)
3349-
env.pop('USERPROFILE', None)
3350-
env.pop('HOMEPATH', None)
3351-
env.pop('HOMEDRIVE', None)
3348+
env.unset('HOME', 'USERPROFILE', 'HOMEPATH', 'HOMEDRIVE')
33523349
env['USERNAME'] = 'alice'
33533350

33543351
# test that the path returns unchanged
@@ -3386,8 +3383,7 @@ def check():
33863383
env['HOMEPATH'] = 'Users\\alice'
33873384
check()
33883385

3389-
env.pop('HOMEDRIVE', None)
3390-
env.pop('HOMEPATH', None)
3386+
env.unset('HOMEDRIVE', 'HOMEPATH')
33913387
env['USERPROFILE'] = 'C:\\Users\\alice'
33923388
check()
33933389

0 commit comments

Comments
 (0)