Skip to content

Commit eb31592

Browse files
committed
[3.13] gh-120782: Update internal type cache when reloading datetime
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.
1 parent abdbf33 commit eb31592

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Lib/test/datetimetester.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6881,6 +6881,23 @@ def pickle_fake_date(datetime_) -> Type[FakeDate]:
68816881
""")
68826882
script_helper.assert_python_ok('-c', script)
68836883

6884+
def test_update_type_cache(self):
6885+
# gh-120782
6886+
script = textwrap.dedent("""
6887+
import sys
6888+
for i in range(5):
6889+
import _datetime
6890+
_datetime.date.max > _datetime.date.min
6891+
_datetime.time.max > _datetime.time.min
6892+
_datetime.datetime.max > _datetime.datetime.min
6893+
_datetime.timedelta.max > _datetime.timedelta.min
6894+
assert isinstance(_datetime.timezone.min, _datetime.tzinfo)
6895+
assert isinstance(_datetime.timezone.utc, _datetime.tzinfo)
6896+
assert isinstance(_datetime.timezone.max, _datetime.tzinfo)
6897+
del sys.modules['_datetime']
6898+
""")
6899+
script_helper.assert_python_ok('-c', script)
6900+
68846901

68856902
def load_tests(loader, standard_tests, pattern):
68866903
standard_tests.addTest(ZoneInfoCompleteTest())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix wrong references of the :mod:`datetime` types after reloading the module.

Modules/_datetimemodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7293,6 +7293,12 @@ _datetime_exec(PyObject *module)
72937293
static_assert(DI100Y == 25 * DI4Y - 1, "DI100Y");
72947294
assert(DI100Y == days_before_year(100+1));
72957295

7296+
if (reloading) {
7297+
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) {
7298+
PyType_Modified(capi_types[i]);
7299+
}
7300+
}
7301+
72967302
if (set_current_module(interp, module) < 0) {
72977303
goto error;
72987304
}

0 commit comments

Comments
 (0)