Skip to content

Commit 8b4fd24

Browse files
authored
gh-128438: Use EnvironmentVarGuard in test_zoneinfo.py (#131870)
1 parent cc05e4b commit 8b4fd24

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
from functools import cached_property
1919

2020
from test.support import MISSING_C_DOCSTRINGS
21+
from test.support.os_helper import EnvironmentVarGuard
2122
from test.test_zoneinfo import _support as test_support
22-
from test.test_zoneinfo._support import OS_ENV_LOCK, TZPATH_TEST_LOCK, ZoneInfoTestBase
23+
from test.test_zoneinfo._support import TZPATH_TEST_LOCK, ZoneInfoTestBase
2324
from test.support.import_helper import import_module, CleanImport
2425

2526
lzma = import_module('lzma')
@@ -1659,24 +1660,9 @@ class TzPathTest(TzPathUserMixin, ZoneInfoTestBase):
16591660
@staticmethod
16601661
@contextlib.contextmanager
16611662
def python_tzpath_context(value):
1662-
path_var = "PYTHONTZPATH"
1663-
unset_env_sentinel = object()
1664-
old_env = unset_env_sentinel
1665-
try:
1666-
with OS_ENV_LOCK:
1667-
old_env = os.environ.get(path_var, None)
1668-
os.environ[path_var] = value
1669-
yield
1670-
finally:
1671-
if old_env is unset_env_sentinel:
1672-
# In this case, `old_env` was never retrieved from the
1673-
# environment for whatever reason, so there's no need to
1674-
# reset the environment TZPATH.
1675-
pass
1676-
elif old_env is None:
1677-
del os.environ[path_var]
1678-
else:
1679-
os.environ[path_var] = old_env # pragma: nocover
1663+
with EnvironmentVarGuard() as env:
1664+
env["PYTHONTZPATH"] = value
1665+
yield
16801666

16811667
def test_env_variable(self):
16821668
"""Tests that the environment variable works with reset_tzpath."""

0 commit comments

Comments
 (0)