https://github.com/python/cpython/commit/8b4fd24ca5e544735f799089188729753817c391
commit: 8b4fd24ca5e544735f799089188729753817c391
branch: main
author: Wulian233 <1055917...@qq.com>
committer: picnixz <10796600+picn...@users.noreply.github.com>
date: 2025-04-27T13:29:37+02:00
summary:

gh-128438: Use `EnvironmentVarGuard` in `test_zoneinfo.py` (#131870)

files:
M Lib/test/test_zoneinfo/test_zoneinfo.py

diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py 
b/Lib/test/test_zoneinfo/test_zoneinfo.py
index fff6c0d865b441..b0dbd768cab7e1 100644
--- a/Lib/test/test_zoneinfo/test_zoneinfo.py
+++ b/Lib/test/test_zoneinfo/test_zoneinfo.py
@@ -18,8 +18,9 @@
 from functools import cached_property
 
 from test.support import MISSING_C_DOCSTRINGS
+from test.support.os_helper import EnvironmentVarGuard
 from test.test_zoneinfo import _support as test_support
-from test.test_zoneinfo._support import OS_ENV_LOCK, TZPATH_TEST_LOCK, 
ZoneInfoTestBase
+from test.test_zoneinfo._support import TZPATH_TEST_LOCK, ZoneInfoTestBase
 from test.support.import_helper import import_module, CleanImport
 
 lzma = import_module('lzma')
@@ -1659,24 +1660,9 @@ class TzPathTest(TzPathUserMixin, ZoneInfoTestBase):
     @staticmethod
     @contextlib.contextmanager
     def python_tzpath_context(value):
-        path_var = "PYTHONTZPATH"
-        unset_env_sentinel = object()
-        old_env = unset_env_sentinel
-        try:
-            with OS_ENV_LOCK:
-                old_env = os.environ.get(path_var, None)
-                os.environ[path_var] = value
-                yield
-        finally:
-            if old_env is unset_env_sentinel:
-                # In this case, `old_env` was never retrieved from the
-                # environment for whatever reason, so there's no need to
-                # reset the environment TZPATH.
-                pass
-            elif old_env is None:
-                del os.environ[path_var]
-            else:
-                os.environ[path_var] = old_env  # pragma: nocover
+        with EnvironmentVarGuard() as env:
+            env["PYTHONTZPATH"] = value
+            yield
 
     def test_env_variable(self):
         """Tests that the environment variable works with reset_tzpath."""

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to