Skip to content

Commit

Permalink
gh-120782: Update internal type cache when reloading datetime (#120829)
Browse files Browse the repository at this point in the history
  • Loading branch information
neonene authored Jun 21, 2024
1 parent 6f1d448 commit a81d434
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -6870,6 +6870,23 @@ def pickle_fake_date(datetime_) -> Type[FakeDate]:
""")
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())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix wrong references of the :mod:`datetime` types after reloading the module.
6 changes: 6 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a81d434

Please sign in to comment.