Skip to content

Commit 0377764

Browse files
authored
Take account of interp restart
1 parent fa0cc40 commit 0377764

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Modules/_datetimemodule.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,21 @@ _get_current_state(PyObject **p_mod)
165165
{
166166
PyInterpreterState *interp = PyInterpreterState_Get();
167167
PyObject *mod = get_current_module(interp, NULL);
168-
assert (mod != NULL);
168+
if (mod == NULL) {
169+
assert(!PyErr_Occurred());
170+
if (PyErr_Occurred()) {
171+
return NULL;
172+
}
173+
/* The static types can outlive the module,
174+
* so we must re-import the module. */
175+
mod = PyImport_ImportModule("_datetime");
176+
if (mod == NULL) {
177+
assert(_Py_IsInterpreterFinalizing(interp));
178+
/* Ask users to take care about the unlikely case
179+
* that happened after restarting the interpreter. */
180+
return NULL;
181+
}
182+
}
169183
datetime_state *st = get_module_state(mod);
170184
*p_mod = mod;
171185
return st;
@@ -2085,6 +2099,9 @@ delta_to_microseconds(PyDateTime_Delta *self)
20852099

20862100
PyObject *current_mod = NULL;
20872101
datetime_state *st = GET_CURRENT_STATE(current_mod);
2102+
if (current_mod == NULL) {
2103+
return NULL;
2104+
}
20882105

20892106
x1 = PyLong_FromLong(GET_TD_DAYS(self));
20902107
if (x1 == NULL)
@@ -2164,6 +2181,9 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
21642181

21652182
PyObject *current_mod = NULL;
21662183
datetime_state *st = GET_CURRENT_STATE(current_mod);
2184+
if (current_mod == NULL) {
2185+
return NULL;
2186+
}
21672187

21682188
tuple = checked_divmod(pyus, CONST_US_PER_SECOND(st));
21692189
if (tuple == NULL) {
@@ -2749,6 +2769,9 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
27492769

27502770
PyObject *current_mod = NULL;
27512771
datetime_state *st = GET_CURRENT_STATE(current_mod);
2772+
if (current_mod == NULL) {
2773+
return NULL;
2774+
}
27522775

27532776
/* Argument objects. */
27542777
PyObject *day = NULL;
@@ -2968,6 +2991,9 @@ delta_total_seconds(PyObject *op, PyObject *Py_UNUSED(dummy))
29682991

29692992
PyObject *current_mod = NULL;
29702993
datetime_state *st = GET_CURRENT_STATE(current_mod);
2994+
if (current_mod == NULL) {
2995+
return NULL;
2996+
}
29712997

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

@@ -3751,6 +3777,9 @@ date_isocalendar(PyObject *self, PyObject *Py_UNUSED(dummy))
37513777

37523778
PyObject *current_mod = NULL;
37533779
datetime_state *st = GET_CURRENT_STATE(current_mod);
3780+
if (current_mod == NULL) {
3781+
return NULL;
3782+
}
37543783

37553784
PyObject *v = iso_calendar_date_new_impl(ISOCALENDAR_DATE_TYPE(st),
37563785
year, week + 1, day + 1);
@@ -6576,6 +6605,9 @@ local_timezone(PyDateTime_DateTime *utc_time)
65766605

65776606
PyObject *current_mod = NULL;
65786607
datetime_state *st = GET_CURRENT_STATE(current_mod);
6608+
if (current_mod == NULL) {
6609+
return NULL;
6610+
}
65796611

65806612
delta = datetime_subtract((PyObject *)utc_time, CONST_EPOCH(st));
65816613
RELEASE_CURRENT_STATE(st, current_mod);
@@ -6820,6 +6852,9 @@ datetime_timestamp(PyObject *op, PyObject *Py_UNUSED(dummy))
68206852
if (HASTZINFO(self) && self->tzinfo != Py_None) {
68216853
PyObject *current_mod = NULL;
68226854
datetime_state *st = GET_CURRENT_STATE(current_mod);
6855+
if (current_mod == NULL) {
6856+
return NULL;
6857+
}
68236858

68246859
PyObject *delta;
68256860
delta = datetime_subtract(op, CONST_EPOCH(st));

0 commit comments

Comments
 (0)