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 865bd6d

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

8 files changed

+25
-32
lines changed

‎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
@@ -676,9 +676,10 @@ def temp_umask(umask):
676676

677677

678678
class EnvironmentVarGuard(collections.abc.MutableMapping):
679+
"""Class to help protect the environment variable properly.
679680
680-
"""Class to help protect the environment variable properly. Can be used as
681-
a context manager."""
681+
Can be used as a context manager.
682+
"""
682683

683684
def __init__(self):
684685
self._environ = os.environ
@@ -712,8 +713,10 @@ def __len__(self):
712713
def set(self, envvar, value):
713714
self[envvar] = value
714715

715-
def unset(self, envvar):
716-
del self[envvar]
716+
def unset(self, envvar, /, *envvars):
717+
"""Unset one or more environment variables."""
718+
for ev in (envvar, *envvars):
719+
del self[ev]
717720

718721
def copy(self):
719722
# We do what os.environ.copy() does.

‎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.py

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

31133113
with os_helper.EnvironmentVarGuard() as env:
3114-
env.pop('HOME', None)
3114+
env.unset('HOME')
31153115

31163116
self.assertEqual(p1.expanduser(), P(userhome) / 'Documents')
31173117
self.assertEqual(p2.expanduser(), P(userhome) / 'Documents')
@@ -3222,10 +3222,7 @@ def test_rglob(self):
32223222
def test_expanduser(self):
32233223
P = self.cls
32243224
with os_helper.EnvironmentVarGuard() as env:
3225-
env.pop('HOME', None)
3226-
env.pop('USERPROFILE', None)
3227-
env.pop('HOMEPATH', None)
3228-
env.pop('HOMEDRIVE', None)
3225+
env.unset('HOME', 'USERPROFILE', 'HOMEPATH', 'HOMEDRIVE')
32293226
env['USERNAME'] = 'alice'
32303227

32313228
# test that the path returns unchanged
@@ -3263,8 +3260,7 @@ def check():
32633260
env['HOMEPATH'] = 'Users\\alice'
32643261
check()
32653262

3266-
env.pop('HOMEDRIVE', None)
3267-
env.pop('HOMEPATH', None)
3263+
env.unset('HOMEDRIVE', 'HOMEPATH')
32683264
env['USERPROFILE'] = 'C:\\Users\\alice'
32693265
check()
32703266

‎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
@@ -304,8 +304,7 @@ def raises_oserror(*a):
304304
with support.swap_attr(platform, '_wmi_query', raises_oserror):
305305
with os_helper.EnvironmentVarGuard() as environ:
306306
try:
307-
if 'PROCESSOR_ARCHITEW6432' in environ:
308-
del environ['PROCESSOR_ARCHITEW6432']
307+
del environ['PROCESSOR_ARCHITEW6432']
309308
environ['PROCESSOR_ARCHITECTURE'] = 'foo'
310309
platform._uname_cache = None
311310
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
@@ -409,8 +409,7 @@ def create_regrtest(self, args):
409409
# which has an unclear API
410410
with os_helper.EnvironmentVarGuard() as env:
411411
# Ignore SOURCE_DATE_EPOCH env var if it's set
412-
if 'SOURCE_DATE_EPOCH' in env:
413-
del env['SOURCE_DATE_EPOCH']
412+
del env['SOURCE_DATE_EPOCH']
414413

415414
regrtest = main.Regrtest(ns)
416415

‎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
@@ -2377,7 +2377,7 @@ def test_environ_path_cwd(self):
23772377

23782378
def test_environ_path_missing(self):
23792379
with os_helper.EnvironmentVarGuard() as env:
2380-
env.pop('PATH', None)
2380+
del env['PATH']
23812381

23822382
# without confstr
23832383
with unittest.mock.patch('os.confstr', side_effect=ValueError, \
@@ -2403,7 +2403,7 @@ def test_empty_path(self):
24032403

24042404
def test_empty_path_no_PATH(self):
24052405
with os_helper.EnvironmentVarGuard() as env:
2406-
env.pop('PATH', None)
2406+
del env['PATH']
24072407
rv = shutil.which(self.file)
24082408
self.assertIsNone(rv)
24092409

@@ -3335,17 +3335,15 @@ def test_stty_match(self):
33353335
expected = (int(size[1]), int(size[0])) # reversed order
33363336

33373337
with os_helper.EnvironmentVarGuard() as env:
3338-
del env['LINES']
3339-
del env['COLUMNS']
3338+
env.unset('LINES', 'COLUMNS')
33403339
actual = shutil.get_terminal_size()
33413340

33423341
self.assertEqual(expected, actual)
33433342

33443343
@unittest.skipIf(support.is_wasi, "WASI has no /dev/null")
33453344
def test_fallback(self):
33463345
with os_helper.EnvironmentVarGuard() as env:
3347-
del env['LINES']
3348-
del env['COLUMNS']
3346+
env.unset('LINES', 'COLUMNS')
33493347

33503348
# sys.__stdout__ has no fileno()
33513349
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
@@ -354,9 +354,7 @@ def test_no_home_directory(self):
354354

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

361359
user_base = site.getuserbase()
362360
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.