]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-102537: Handle check for PYTHONTZPATH failing in zoneinfo test (GH-102538)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 10 Mar 2023 15:54:45 +0000 (07:54 -0800)
committerGitHub <noreply@github.com>
Fri, 10 Mar 2023 15:54:45 +0000 (07:54 -0800)
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.
(cherry picked from commit 64bde502cf89963bc7382b03ea9e1c0967d22e35)

Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
Lib/test/test_zoneinfo/test_zoneinfo.py
Misc/NEWS.d/next/Tests/2023-03-08-13-54-20.gh-issue-102537.Vfplpb.rst [new file with mode: 0644]

index a2172f3ac21d08019b03fd0cad8dde9ddca4976d..fc19247b262e01f0c5b8b042cef4046336a6c22e 100644 (file)
@@ -1530,13 +1530,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
diff --git a/Misc/NEWS.d/next/Tests/2023-03-08-13-54-20.gh-issue-102537.Vfplpb.rst b/Misc/NEWS.d/next/Tests/2023-03-08-13-54-20.gh-issue-102537.Vfplpb.rst
new file mode 100644 (file)
index 0000000..94d160d
--- /dev/null
@@ -0,0 +1,2 @@
+Adjust the error handling strategy in
+``test_zoneinfo.TzPathTest.python_tzpath_context``. Patch by Paul Ganssle.