|
18 | 18 | from functools import cached_property |
19 | 19 |
|
20 | 20 | from test.support import MISSING_C_DOCSTRINGS |
| 21 | +from test.support.os_helper import EnvironmentVarGuard |
21 | 22 | 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 |
23 | 24 | from test.support.import_helper import import_module, CleanImport |
24 | 25 |
|
25 | 26 | lzma = import_module('lzma') |
@@ -1659,24 +1660,9 @@ class TzPathTest(TzPathUserMixin, ZoneInfoTestBase): |
1659 | 1660 | @staticmethod |
1660 | 1661 | @contextlib.contextmanager |
1661 | 1662 | 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 |
1680 | 1666 |
|
1681 | 1667 | def test_env_variable(self): |
1682 | 1668 | """Tests that the environment variable works with reset_tzpath.""" |
|
0 commit comments