]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-120782: Update internal type cache when reloading datetime
authorneonene <53406459+neonene@users.noreply.github.com>
Fri, 21 Jun 2024 19:09:24 +0000 (04:09 +0900)
committerneonene <53406459+neonene@users.noreply.github.com>
Fri, 21 Jun 2024 19:09:24 +0000 (04:09 +0900)
When reloading _datetime module, the single-phase version did not invoke the PyInit__datetime function, whereas the current multi-phase version updates the static types through the module init. The outdated static type cache in the interpreter state needs to be invalidated at the end of reloading the multi-phase module.

Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2024-06-21-12-00-16.gh-issue-120782.LOE8tj.rst [new file with mode: 0644]
Modules/_datetimemodule.c

index 77472d84e60ca3df371db116cc26c2539f38cdaf..2b676def196de8a9e62c20cb104b1d6667f4b535 100644 (file)
@@ -6881,6 +6881,23 @@ class ExtensionModuleTests(unittest.TestCase):
                 """)
             script_helper.assert_python_ok('-c', script)
 
+    def test_update_type_cache(self):
+        # gh-120782
+        script = textwrap.dedent("""
+            import sys
+            for i in range(5):
+                import _datetime
+                _datetime.date.max > _datetime.date.min
+                _datetime.time.max > _datetime.time.min
+                _datetime.datetime.max > _datetime.datetime.min
+                _datetime.timedelta.max > _datetime.timedelta.min
+                assert isinstance(_datetime.timezone.min, _datetime.tzinfo)
+                assert isinstance(_datetime.timezone.utc, _datetime.tzinfo)
+                assert isinstance(_datetime.timezone.max, _datetime.tzinfo)
+                del sys.modules['_datetime']
+            """)
+        script_helper.assert_python_ok('-c', script)
+
 
 def load_tests(loader, standard_tests, pattern):
     standard_tests.addTest(ZoneInfoCompleteTest())
diff --git a/Misc/NEWS.d/next/Library/2024-06-21-12-00-16.gh-issue-120782.LOE8tj.rst b/Misc/NEWS.d/next/Library/2024-06-21-12-00-16.gh-issue-120782.LOE8tj.rst
new file mode 100644 (file)
index 0000000..02acbd2
--- /dev/null
@@ -0,0 +1 @@
+Fix wrong references of the :mod:`datetime` types after reloading the module.
index 135d6cb683f0d229c93a8b7fbcd7e571c8ba4e38..5388e39ca02a2d80e602e279ee819be15d301c63 100644 (file)
@@ -7293,6 +7293,12 @@ _datetime_exec(PyObject *module)
     static_assert(DI100Y == 25 * DI4Y - 1, "DI100Y");
     assert(DI100Y == days_before_year(100+1));
 
+    if (reloading) {
+        for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
+            PyType_Modified(capi_types[i]);
+        }
+    }
+
     if (set_current_module(interp, module) < 0) {
         goto error;
     }