8000 Clear the "current" module when finalizing the module. · python/cpython@5a8b1aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a8b1aa

Browse files
Clear the "current" module when finalizing the module.
1 parent ede4415 commit 5a8b1aa

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

Modules/_datetimemodule.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,41 @@ set_current_module(PyInterpreterState *interp, PyObject *mod)
136136
return PyDict_SetItemString(dict, INTERP_KEY, mod);
137137
}
138138

139+
static void
140+
clear_current_module(PyInterpreterState *interp, PyObject *expected)
141+
{
142+
PyObject *exc = PyErr_GetRaisedException();
143+
144+
PyObject *dict = PyInterpreterState_GetDict(interp);
145+
if (dict == NULL) {
146+
goto error;
147+
}
148+
149+
if (expected != NULL) {
150+
PyObject *current = NULL;
151+
if (PyDict_GetItemStringRef(dict, INTERP_KEY, &current) < 0) {
152+
goto error;
153+
}
154+
if (current != expected) {
155+
goto finally;
156+
}
157+
}
158+
159+
if (PyDict_DelItemString(dict, INTERP_KEY) < 0) {
160+
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
161+
goto error;
162+
}
163+
}
164+
165+
goto finally;
166+
167+
error:
168+
PyErr_Print();
169+
170+
finally:
171+
PyErr_SetRaisedException(exc);
172+
}
173+
139174

140175
/* We require that C int be at least 32 bits, and use int virtually
141176
* everywhere. In just a few cases we use a temp long, where a Python
@@ -7199,14 +7234,21 @@ module_clear(PyObject *mod)
71997234
{
72007235
datetime_state *st = get_module_state(mod);
72017236
clear_state(st);
7237+
7238+
PyInterpreterState *interp = PyInterpreterState_Get();
7239+
clear_current_module(interp, mod);
7240+
72027241
return 0;
72037242
}
72047243

72057244
static void
72067245
module_free(void *mod)
72077246
{
7208-
datetime_state *st = get_module_state(mod);
7247+
datetime_state *st = get_module_state((PyObject *)mod);
72097248
clear_state(st);
7249+
7250+
PyInterpreterState *interp = PyInterpreterState_Get();
7251+
clear_current_module(interp, (PyObject *)mod);
72107252
}
72117253

72127254
static PyModuleDef datetimemodule = {

0 commit comments

Comments
 (0)
0