Skip to content

Commit 76cc153

Browse files
authored
Respect interp-dict held in module state
1 parent 5141a76 commit 76cc153

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

Modules/_datetimemodule.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "Python.h"
1313
#include "pycore_long.h" // _PyLong_GetOne()
1414
#include "pycore_object.h" // _PyObject_Init()
15-
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
1615
#include "pycore_time.h" // _PyTime_ObjectToTime_t()
1716
#include "pycore_unicodeobject.h" // _PyUnicode_Copy()
1817

@@ -162,7 +161,6 @@ static datetime_state *
162161
_get_current_state(PyObject **p_mod)
163162
{
164163
PyInterpreterState *interp = PyInterpreterState_Get();
165-
assert(interp->dict != NULL);
166164
PyObject *mod = get_current_module(interp, NULL);
167165
if (mod == NULL) {
168166
assert(!PyErr_Occurred());
@@ -187,11 +185,10 @@ _get_current_state(PyObject **p_mod)
187185
Py_DECREF(MOD_VAR)
188186

189187
static int
190-
set_current_module(PyInterpreterState *interp, PyObject *mod)
188+
set_current_module(datetime_state *st, PyObject *mod)
191189
{
192-
assert(interp->dict != NULL);
193190
assert(mod != NULL);
194-
PyObject *dict = PyInterpreterState_GetDict(interp);
191+
PyObject *dict = st->interp_dict;
195192
if (dict == NULL) {
196193
return -1;
197194
}
@@ -200,19 +197,13 @@ set_current_module(PyInterpreterState *interp, PyObject *mod)
200197
}
201198

202199
static void
203-
clear_current_module(PyInterpreterState *interp, PyObject *expected)
200+
clear_current_module(datetime_state *st, PyObject *expected)
204201
{
205-
if (interp->dict == NULL) {
206-
// Do not resurrect a dict during interp-shutdown to avoid the leak
207-
assert(_Py_IsInterpreterFinalizing(interp));
208-
return;
209-
}
210-
211202
PyObject *exc = PyErr_GetRaisedException();
212203

213-
PyObject *dict = PyInterpreterState_GetDict(interp);
204+
PyObject *dict = st->interp_dict;
214205
if (dict == NULL) {
215-
goto error;
206+
return; /* Already cleared */
216207
}
217208

218209
if (expected != NULL) {
@@ -7206,8 +7197,15 @@ create_timezone_from_delta(int days, int sec, int ms, int normalize)
72067197
*/
72077198

72087199
static int
7209-
init_state(datetime_state *st, PyObject *module, PyObject *old_module)
7200+
init_state(datetime_state *st,
7201+
PyInterpreterState *interp, PyObject *module, PyObject *old_module)
72107202
{
7203+
PyObject *dict = PyInterpreterState_GetDict(interp);
7204+
if (dict == NULL) {
7205+
return -1;
7206+
}
7207+
st->interp_dict = Py_NewRef(dict);
7208+
72117209
/* Each module gets its own heap types. */
72127210
#define ADD_TYPE(FIELD, SPEC, BASE) \
72137211
do { \
@@ -7226,6 +7224,7 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module)
72267224
assert(old_module != module);
72277225
datetime_state *st_old = get_module_state(old_module);
72287226
*st = (datetime_state){
7227+
.interp_dict = st->interp_dict,
72297228
.isocalendar_date_type = st->isocalendar_date_type,
72307229
.us_per_ms = Py_NewRef(st_old->us_per_ms),
72317230
.us_per_second = Py_NewRef(st_old->us_per_second),
@@ -7279,13 +7278,6 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module)
72797278
return -1;
72807279
}
72817280

7282-
PyInterpreterState *interp = PyInterpreterState_Get();
7283-
PyObject *dict = PyInterpreterState_GetDict(interp);
7284-
if (dict == NULL) {
7285-
return -1;
7286-
}
7287-
st->interp_dict = Py_NewRef(dict);
7288-
72897281
return 0;
72907282
}
72917283

@@ -7379,7 +7371,7 @@ _datetime_exec(PyObject *module)
73797371
}
73807372
}
73817373

7382-
if (init_state(st, module, old_module) < 0) {
7374+
if (init_state(st, interp, module, old_module) < 0) {
73837375
goto error;
73847376
}
73857377

@@ -7485,7 +7477,7 @@ _datetime_exec(PyObject *module)
74857477
static_assert(DI100Y == 25 * DI4Y - 1, "DI100Y");
74867478
assert(DI100Y == days_before_year(100+1));
74877479

7488-
if (set_current_module(interp, module) < 0) {
7480+
if (set_current_module(st, module) < 0) {
74897481
goto error;
74907482
}
74917483

@@ -7518,10 +7510,8 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)
75187510
static int
75197511
module_clear(PyObject *mod)
75207512
{
7521-
PyInterpreterState *interp = PyInterpreterState_Get();
7522-
clear_current_module(interp, mod);
7523-
75247513
datetime_state *st = get_module_state(mod);
7514+
clear_current_module(st, mod);
75257515
clear_state(st);
75267516

75277517
// The runtime takes care of the static types for us.

0 commit comments

Comments
 (0)