Thanks to visit codestin.com
Credit goes to github.com

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

Lib/test/test_platform.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ def raises_oserror(*a):
368368
with support.swap_attr(platform, '_wmi_query', raises_oserror):
369369
with os_helper.EnvironmentVarGuard() as environ:
370370
try:
371-
if 'PROCESSOR_ARCHITEW6432' in environ:
372-
del environ['PROCESSOR_ARCHITEW6432']
371+
del environ['PROCESSOR_ARCHITEW6432']
373372
environ['PROCESSOR_ARCHITECTURE'] = 'foo'
374373
platform._uname_cache = None
375374
system, node, release, version, machine, processor = platform.uname()

Lib/test/test_regrtest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,7 @@ def create_regrtest(self, args):
422422
# which has an unclear API
423423
with os_helper.EnvironmentVarGuard() as env:
424424
# Ignore SOURCE_DATE_EPOCH env var if it's set
425-
if 'SOURCE_DATE_EPOCH' in env:
426-
del env['SOURCE_DATE_EPOCH']
425+
del env['SOURCE_DATE_EPOCH']
427426

428427
regrtest = main.Regrtest(ns)
429428

Lib/test/test_shutil.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ def test_environ_path_cwd(self):
24582458

24592459
def test_environ_path_missing(self):
24602460
with os_helper.EnvironmentVarGuard() as env:
2461-
env.pop('PATH', None)
2461+
del env['PATH']
24622462

24632463
# without confstr
24642464
with unittest.mock.patch('os.confstr', side_effect=ValueError, \
@@ -2484,7 +2484,7 @@ def test_empty_path(self):
24842484

24852485
def test_empty_path_no_PATH(self):
24862486
with os_helper.EnvironmentVarGuard() as env:
2487-
env.pop('PATH', None)
2487+
del env['PATH']
24882488
rv = shutil.which(self.file)
24892489
self.assertIsNone(rv)
24902490

@@ -3446,17 +3446,15 @@ def test_stty_match(self):
34463446
expected = (int(size[1]), int(size[0])) # reversed order
34473447

34483448
with os_helper.EnvironmentVarGuard() as env:
3449-
del env['LINES']
3450-
del env['COLUMNS']
3449+
env.unset('LINES', 'COLUMNS')
34513450
actual = shutil.get_terminal_size()
34523451

34533452
self.assertEqual(expected, actual)
34543453

34553454
@unittest.skipIf(support.is_wasi, "WASI has no /dev/null")
34563455
def test_fallback(self):
34573456
with os_helper.EnvironmentVarGuard() as env:
3458-
del env['LINES']
3459-
del env['COLUMNS']
3457+
env.unset('LINES', 'COLUMNS')
34603458

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

Lib/test/test_site.py

Lines 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),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Allow to unset one or more environment variables at once via
2+
:meth:`EnvironmentVarGuard.unset()
3+
<test.support.os_helper.EnvironmentVarGuard.unset>`. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)