Skip to content

Commit 10afa50

Browse files
committed
Use EnvironmentVarGuard for test_zoneinfo.py to manage environment varibales
1 parent 9c1e85f commit 10afa50

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

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

0 commit comments

Comments
 (0)