]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-137754: Fix import of zoneinfo if _datetime is not available (GH-137845...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 23 Aug 2025 09:33:58 +0000 (11:33 +0200)
committerGitHub <noreply@github.com>
Sat, 23 Aug 2025 09:33:58 +0000 (09:33 +0000)
Both modules should use the Python implementation in that case.
(cherry picked from commit 6620ef0ff62dea2632bedf9afa88aa4780a2c9d9)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_zoneinfo/test_zoneinfo.py
Lib/zoneinfo/__init__.py
Misc/NEWS.d/next/Library/2025-08-16-09-02-11.gh-issue-137754.mCev1Y.rst [new file with mode: 0644]

index d51736e34c60fce3e70670856fab153d70c9f254..f7bb2aa151c04a3b858bcf73d03637fb5d62d698 100644 (file)
@@ -21,6 +21,7 @@ from test.support import MISSING_C_DOCSTRINGS, requires_gil_enabled
 from test.test_zoneinfo import _support as test_support
 from test.test_zoneinfo._support import OS_ENV_LOCK, TZPATH_TEST_LOCK, ZoneInfoTestBase
 from test.support.import_helper import import_module, CleanImport
+from test.support.script_helper import assert_python_ok
 
 lzma = import_module('lzma')
 py_zoneinfo, c_zoneinfo = test_support.get_modules()
@@ -1960,6 +1961,26 @@ class CTestModule(TestModule):
     module = c_zoneinfo
 
 
+class MiscTests(unittest.TestCase):
+    def test_pydatetime(self):
+        # Test that zoneinfo works if the C implementation of datetime
+        # is not available and the Python implementation of datetime is used.
+        # The Python implementation of zoneinfo should be used in thet case.
+        #
+        # Run the test in a subprocess, as importing _zoneinfo with
+        # _datettime disabled causes crash in the previously imported
+        # _zoneinfo.
+        assert_python_ok('-c', '''if 1:
+            import sys
+            sys.modules['_datetime'] = None
+            import datetime
+            import zoneinfo
+            tzinfo = zoneinfo.ZoneInfo('Europe/London')
+            datetime.datetime(2025, 10, 26, 2, 0, tzinfo=tzinfo)
+            ''',
+            PYTHONTZPATH=str(ZONEINFO_DATA.tzpath))
+
+
 class ExtensionBuiltTest(unittest.TestCase):
     """Smoke test to ensure that the C and Python extensions are both tested.
 
index f5510ee049751345ef4788401ca427549be3aab5..df2ae909f53cf29a49ec35a051e03ad7c37204fa 100644 (file)
@@ -12,7 +12,10 @@ from ._common import ZoneInfoNotFoundError
 
 try:
     from _zoneinfo import ZoneInfo
-except ImportError:  # pragma: nocover
+except (ImportError, AttributeError):  # pragma: nocover
+    # AttributeError: module 'datetime' has no attribute 'datetime_CAPI'.
+    # This happens when the '_datetime' module is not available and the
+    # pure Python implementation is used instead.
     from ._zoneinfo import ZoneInfo
 
 reset_tzpath = _tzpath.reset_tzpath
diff --git a/Misc/NEWS.d/next/Library/2025-08-16-09-02-11.gh-issue-137754.mCev1Y.rst b/Misc/NEWS.d/next/Library/2025-08-16-09-02-11.gh-issue-137754.mCev1Y.rst
new file mode 100644 (file)
index 0000000..323870a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix import of the :mod:`zoneinfo` module if the C implementation of the
+:mod:`datetime` module is not available.