Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    assert isinstance(_datetime.timezone.min, _datetime.tzinfo)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
    assert not isinstance(_datetime.timezone.min, _datetime.tzinfo)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Both fail, so this checks if it crashes or not. I agree this is not a good example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the code looks a bit unusual, it would be worth adding a brief comment explaining that.

(Sorry I didn't notice this during my earlier review. Good catch, @JelleZijlstra.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to have these asserts actually check that the values are of the type they should be? The bug manifested itself as these values appearing to be objects of various random types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also assert that timezone.__dict__["min"] is timezone.min.

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