Skip to content

Commit 5141a76

Browse files
authored
Safer ref cycle between mod and interp-dict
1 parent e6aa018 commit 5141a76

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Modules/_datetimemodule.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ typedef struct {
5050

5151
/* The interned Unix epoch datetime instance */
5252
PyObject *epoch;
53+
54+
/* Interpreter's dict holds the module */
55+
PyObject *interp_dict;
5356
} datetime_state;
5457

5558
/* The module has a fixed number of static objects, due to being exposed
@@ -7276,6 +7279,13 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module)
72767279
return -1;
72777280
}
72787281

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+
72797289
return 0;
72807290
}
72817291

@@ -7284,6 +7294,7 @@ traverse_state(datetime_state *st, visitproc visit, void *arg)
72847294
{
72857295
/* heap types */
72867296
Py_VISIT(st->isocalendar_date_type);
7297+
Py_VISIT(st->interp_dict);
72877298

72887299
return 0;
72897300
}
@@ -7300,6 +7311,7 @@ clear_state(datetime_state *st)
73007311
Py_CLEAR(st->us_per_week);
73017312
Py_CLEAR(st->seconds_per_day);
73027313
Py_CLEAR(st->epoch);
7314+
Py_CLEAR(st->interp_dict);
73037315
return 0;
73047316
}
73057317

@@ -7506,12 +7518,12 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)
75067518
static int
75077519
module_clear(PyObject *mod)
75087520
{
7509-
datetime_state *st = get_module_state(mod);
7510-
clear_state(st);
7511-
75127521
PyInterpreterState *interp = PyInterpreterState_Get();
75137522
clear_current_module(interp, mod);
75147523

7524+
datetime_state *st = get_module_state(mod);
7525+
clear_state(st);
7526+
75157527
// The runtime takes care of the static types for us.
75167528
// See _PyTypes_FiniExtTypes()..
75177529

0 commit comments

Comments
 (0)