Skip to content

gh-132413: Check if datetime current state is not NULL #136321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
19 changes: 19 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7295,6 +7295,25 @@ def test_update_type_cache(self):
""")
script_helper.assert_python_ok('-c', script)

def test_static_type_at_shutdown(self):
# gh-132413
script = textwrap.dedent("""
import _datetime
timedelta = _datetime.timedelta

def gen():
try:
yield
finally:
# sys.modules is empty
_datetime.timedelta(days=1)
timedelta(days=1)

it = gen()
next(it)
""")
script_helper.assert_python_ok('-c', script)


def load_tests(loader, standard_tests, pattern):
standard_tests.addTest(ZoneInfoCompleteTest())
Expand Down
22 changes: 22 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,9 @@ delta_to_microseconds(PyDateTime_Delta *self)

PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
return NULL;
}

x1 = PyLong_FromLong(GET_TD_DAYS(self));
if (x1 == NULL)
Expand Down Expand Up @@ -2194,6 +2197,9 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)

PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
return NULL;
}

tuple = checked_divmod(pyus, CONST_US_PER_SECOND(st));
if (tuple == NULL) {
Expand Down Expand Up @@ -2779,6 +2785,9 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)

PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
return NULL;
}

/* Argument objects. */
PyObject *day = NULL;
Expand Down Expand Up @@ -2998,6 +3007,10 @@ delta_total_seconds(PyObject *op, PyObject *Py_UNUSED(dummy))

PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
Py_DECREF(total_microseconds);
return NULL;
}

total_seconds = PyNumber_TrueDivide(total_microseconds, CONST_US_PER_SECOND(st));

Expand Down Expand Up @@ -3781,6 +3794,9 @@ date_isocalendar(PyObject *self, PyObject *Py_UNUSED(dummy))

PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
return NULL;
}

PyObject *v = iso_calendar_date_new_impl(ISOCALENDAR_DATE_TYPE(st),
year, week + 1, day + 1);
Expand Down Expand Up @@ -6616,6 +6632,9 @@ local_timezone(PyDateTime_DateTime *utc_time)

PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
return NULL;
}

delta = datetime_subtract((PyObject *)utc_time, CONST_EPOCH(st));
RELEASE_CURRENT_STATE(st, current_mod);
Expand Down Expand Up @@ -6860,6 +6879,9 @@ datetime_timestamp(PyObject *op, PyObject *Py_UNUSED(dummy))
if (HASTZINFO(self) && self->tzinfo != Py_None) {
PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
if (st == NULL) {
return NULL;
}

PyObject *delta;
delta = datetime_subtract(op, CONST_EPOCH(st));
Expand Down
Loading