diff --git a/Python/pystate.c b/Python/pystate.c --- a/Python/pystate.c +++ b/Python/pystate.c @@ -249,21 +249,24 @@ PyState_FindModule(struct PyModuleDef* m { Py_ssize_t index = module->m_base.m_index; PyInterpreterState *state = PyThreadState_GET()->interp; - PyObject *res; + PyObject *wr; if (index == 0) return NULL; if (state->modules_by_index == NULL) return NULL; if (index >= PyList_GET_SIZE(state->modules_by_index)) return NULL; - res = PyList_GET_ITEM(state->modules_by_index, index); - return res==Py_None ? NULL : res; + wr = PyList_GET_ITEM(state->modules_by_index, index); + if (wr == Py_None) + return NULL; + return PyWeakref_GET_OBJECT(wr); } int _PyState_AddModule(PyObject* module, struct PyModuleDef* def) { PyInterpreterState *state = PyThreadState_GET()->interp; + PyObject *wr; if (!def) return -1; if (!state->modules_by_index) { @@ -274,9 +277,9 @@ int while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) if (PyList_Append(state->modules_by_index, Py_None) < 0) return -1; - Py_INCREF(module); + wr = PyWeakref_NewRef(module, NULL); return PyList_SetItem(state->modules_by_index, - def->m_base.m_index, module); + def->m_base.m_index, wr); } int @@ -290,8 +293,9 @@ PyState_AddModule(PyObject* module, stru } index = def->m_base.m_index; if (state->modules_by_index) { - if(PyList_GET_SIZE(state->modules_by_index) >= index) { - if(module == PyList_GET_ITEM(state->modules_by_index, index)) { + if (PyList_GET_SIZE(state->modules_by_index) >= index) { + PyObject *wr = PyList_GET_ITEM(state->modules_by_index, index); + if (wr != Py_None && module == PyWeakref_GET_OBJECT(wr)) { Py_FatalError("PyState_AddModule: Module already added!"); return -1; }