summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_zoneinfo
diff options
context:
space:
mode:
authorPaul Ganssle <1377457+pganssle@users.noreply.github.com>2023-03-10 15:29:37 (GMT)
committerGitHub <noreply@github.com>2023-03-10 15:29:37 (GMT)
commit64bde502cf89963bc7382b03ea9e1c0967d22e35 (patch)
treedde32b0e54a44f0ab0e293cfe8d5fdb046f9b789 /Lib/test/test_zoneinfo
parent53dceb53ade15587b9cfd30c0a0942232517dee9 (diff)
downloadcpython-64bde502cf89963bc7382b03ea9e1c0967d22e35.zip
cpython-64bde502cf89963bc7382b03ea9e1c0967d22e35.tar.gz
cpython-64bde502cf89963bc7382b03ea9e1c0967d22e35.tar.bz2
GH-102537: Handle check for PYTHONTZPATH failing in zoneinfo test (GH-102538)
It is possible but unlikely for the `python_tzpath_context` function to fail between the start of the `try` block and the point where `os.environ.get` succeeds, in which case `old_env` will be undefined. In this case, we want to take no action. Practically speaking this will really only happen in an error condition anyway, so it doesn't really matter, but we should probably do it right anyway.
Diffstat (limited to 'Lib/test/test_zoneinfo')
-rw-r--r--Lib/test/test_zoneinfo/test_zoneinfo.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py
index 82041a2..ae921f7 100644
--- a/Lib/test/test_zoneinfo/test_zoneinfo.py
+++ b/Lib/test/test_zoneinfo/test_zoneinfo.py
@@ -1543,13 +1543,20 @@ class TzPathTest(TzPathUserMixin, ZoneInfoTestBase):
@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 None:
+ 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