Skip to content

Commit 0946ed2

Browse files
authored
gh-128438: Add EnvironmentVarGuard for test_pdb.py (#128522)
1 parent 3a570c6 commit 0946ed2

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

Lib/test/test_pdb.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,16 +3152,12 @@ def run_pdb_script(self, script, commands,
31523152
self.addCleanup(os_helper.unlink, '.pdbrc')
31533153
self.addCleanup(os_helper.unlink, filename)
31543154

3155-
homesave = None
3156-
if remove_home:
3157-
homesave = os.environ.pop('HOME', None)
3158-
try:
3155+
with os_helper.EnvironmentVarGuard() as env:
3156+
if remove_home:
3157+
env.unset('HOME')
31593158
if script_args is None:
31603159
script_args = []
31613160
stdout, stderr = self._run_pdb([filename] + script_args, commands, expected_returncode, extra_env)
3162-
finally:
3163-
if homesave is not None:
3164-
os.environ['HOME'] = homesave
31653161
return stdout, stderr
31663162

31673163
def run_pdb_module(self, script, commands):
@@ -3585,17 +3581,14 @@ def test_readrc_kwarg(self):
35853581
self.assertIn("NameError: name 'invalid' is not defined", stdout)
35863582

35873583
def test_readrc_homedir(self):
3588-
save_home = os.environ.pop("HOME", None)
3589-
with os_helper.temp_dir() as temp_dir, patch("os.path.expanduser"):
3590-
rc_path = os.path.join(temp_dir, ".pdbrc")
3591-
os.path.expanduser.return_value = rc_path
3592-
try:
3584+
with os_helper.EnvironmentVarGuard() as env:
3585+
env.unset("HOME")
3586+
with os_helper.temp_dir() as temp_dir, patch("os.path.expanduser"):
3587+
rc_path = os.path.join(temp_dir, ".pdbrc")
3588+
os.path.expanduser.return_value = rc_path
35933589
with open(rc_path, "w") as f:
35943590
f.write("invalid")
35953591
self.assertEqual(pdb.Pdb().rcLines[0], "invalid")
3596-
finally:
3597-
if save_home is not None:
3598-
os.environ["HOME"] = save_home
35993592

36003593
def test_header(self):
36013594
stdout = StringIO()

0 commit comments

Comments
 (0)