Skip to content

Commit 64bde50

Browse files
authored
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.
1 parent 53dceb5 commit 64bde50

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,13 +1543,20 @@ class TzPathTest(TzPathUserMixin, ZoneInfoTestBase):
15431543
@contextlib.contextmanager
15441544
def python_tzpath_context(value):
15451545
path_var = "PYTHONTZPATH"
1546+
unset_env_sentinel = object()
1547+
old_env = unset_env_sentinel
15461548
try:
15471549
with OS_ENV_LOCK:
15481550
old_env = os.environ.get(path_var, None)
15491551
os.environ[path_var] = value
15501552
yield
15511553
finally:
1552-
if old_env is None:
1554+
if old_env is unset_env_sentinel:
1555+
# In this case, `old_env` was never retrieved from the
1556+
# environment for whatever reason, so there's no need to
1557+
# reset the environment TZPATH.
1558+
pass
1559+
elif old_env is None:
15531560
del os.environ[path_var]
15541561
else:
15551562
os.environ[path_var] = old_env # pragma: nocover
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Adjust the error handling strategy in
2+
``test_zoneinfo.TzPathTest.python_tzpath_context``. Patch by Paul Ganssle.

0 commit comments

Comments
 (0)