https://github.com/python/cpython/commit/a096a41b922ac90bfd7bf23852cf0a32a4f54faf
commit: a096a41b922ac90bfd7bf23852cf0a32a4f54faf
branch: main
author: Wulian233 <1055917...@qq.com>
committer: Eclips4 <kirill.ba...@mail.ru>
date: 2025-03-27T18:07:09+02:00
summary:

gh-128438: Use `EnvironmentVarGuard` for `datetimetester.py` to manage 
environment varibales (#130002)

files:
M Lib/test/datetimetester.py

diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index f9d20ef9c626a9..ecb37250ceb6c4 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -1,5 +1,6 @@
 """Test the datetime module."""
 import bisect
+import contextlib
 import copy
 import decimal
 import io
@@ -20,7 +21,7 @@
 
 from test import support
 from test.support import is_resource_enabled, ALWAYS_EQ, LARGEST, SMALLEST
-from test.support import script_helper, warnings_helper
+from test.support import os_helper, script_helper, warnings_helper
 
 import datetime as datetime_module
 from datetime import MINYEAR, MAXYEAR
@@ -6691,6 +6692,17 @@ def test_gaps(self):
                 ldt = tz.fromutc(udt.replace(tzinfo=tz))
                 self.assertEqual(ldt.fold, 0)
 
+    @classmethod
+    @contextlib.contextmanager
+    def _change_tz(cls, new_tzinfo):
+        try:
+            with os_helper.EnvironmentVarGuard() as env:
+                env["TZ"] = new_tzinfo
+                _time.tzset()
+                yield
+        finally:
+            _time.tzset()
+
     @unittest.skipUnless(
         hasattr(_time, "tzset"), "time module has no attribute tzset"
     )
@@ -6705,10 +6717,7 @@ def test_system_transitions(self):
                 self.zonename.startswith('right/')):
             self.skipTest("Skipping %s" % self.zonename)
         tz = self.tz
-        TZ = os.environ.get('TZ')
-        os.environ['TZ'] = self.zonename
-        try:
-            _time.tzset()
+        with self._change_tz(self.zonename):
             for udt, shift in tz.transitions():
                 if udt.year >= 2037:
                     # System support for times around the end of 32-bit time_t
@@ -6716,7 +6725,7 @@ def test_system_transitions(self):
                     break
                 s0 = (udt - datetime(1970, 1, 1)) // SEC
                 ss = shift // SEC   # shift seconds
-                for x in [-40 * 3600, -20*3600, -1, 0,
+                for x in [-40 * 3600, -20 * 3600, -1, 0,
                           ss - 1, ss + 20 * 3600, ss + 40 * 3600]:
                     s = s0 + x
                     sdt = datetime.fromtimestamp(s)
@@ -6735,12 +6744,6 @@ def test_system_transitions(self):
                     utc0 = dt.astimezone(timezone.utc)
                     utc1 = dt.replace(fold=1).astimezone(timezone.utc)
                     self.assertEqual(utc0, utc1 + timedelta(0, ss))
-        finally:
-            if TZ is None:
-                del os.environ['TZ']
-            else:
-                os.environ['TZ'] = TZ
-            _time.tzset()
 
 
 class ZoneInfoCompleteTest(unittest.TestSuite):

_______________________________________________
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