summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorneonene <53406459+neonene@users.noreply.github.com>2024-06-21 17:09:33 (GMT)
committerGitHub <noreply@github.com>2024-06-21 17:09:33 (GMT)
commita81d434c06335b0989ba83666ec7076b9d9d4e1e (patch)
treefa2a3dfa9953bb706066b201ef556e90a3a03685
parent6f1d448bc110633eda110310fd833bd46e7b30f2 (diff)
downloadcpython-a81d434c06335b0989ba83666ec7076b9d9d4e1e.zip
cpython-a81d434c06335b0989ba83666ec7076b9d9d4e1e.tar.gz
cpython-a81d434c06335b0989ba83666ec7076b9d9d4e1e.tar.bz2
gh-120782: Update internal type cache when reloading datetime (#120829)
-rw-r--r--Lib/test/datetimetester.py17
-rw-r--r--Misc/NEWS.d/next/Library/2024-06-21-12-00-16.gh-issue-120782.LOE8tj.rst1
-rw-r--r--Modules/_datetimemodule.c6
3 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index e55b738..b8f69e7 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -6870,6 +6870,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
+ isinstance(_datetime.timezone.min, _datetime.tzinfo)
+ isinstance(_datetime.timezone.utc, _datetime.tzinfo)
+ 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
index 0000000..02acbd2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-06-21-12-00-16.gh-issue-120782.LOE8tj.rst
@@ -0,0 +1 @@
+Fix wrong references of the :mod:`datetime` types after reloading the module.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 31bf641..85595dc 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -7296,6 +7296,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;
}