bpo-38858: _PyImport_FixupExtensionObject() handles subinterpreters by vstinner · Pull Request #17350 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Include/cpython/pystate.h
6 changes: 6 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);

PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);


PyAPI_FUNC(int) _PyState_AddModule(
PyThreadState *tstate,
PyObject* module,
struct PyModuleDef* def);

#ifdef __cplusplus
}
#endif
Expand Down
70 changes: 41 additions & 29 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,50 +695,62 @@ int
_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
PyObject *filename, PyObject *modules)
{
PyObject *dict, *key;
struct PyModuleDef *def;
int res;
if (extensions == NULL) {
extensions = PyDict_New();
if (extensions == NULL)
return -1;
}
if (mod == NULL || !PyModule_Check(mod)) {
PyErr_BadInternalCall();
return -1;
}
def = PyModule_GetDef(mod);

struct PyModuleDef *def = PyModule_GetDef(mod);
if (!def) {
PyErr_BadInternalCall();
return -1;
}
if (PyObject_SetItem(modules, name, mod) < 0)

PyThreadState *tstate = _PyThreadState_GET();
if (PyObject_SetItem(modules, name, mod) < 0) {
return -1;
if (_PyState_AddModule(mod, def) < 0) {
}
if (_PyState_AddModule(tstate, mod, def) < 0) {
PyMapping_DelItem(modules, name);
return -1;
}
if (def->m_size == -1) {
if (def->m_base.m_copy) {
/* Somebody already imported the module,
likely under a different name.
XXX this should really not happen. */
Py_CLEAR(def->m_base.m_copy);

if (_Py_IsMainInterpreter(tstate)) {
if (def->m_size == -1) {
if (def->m_base.m_copy) {
/* Somebody already imported the module,
likely under a different name.
XXX this should really not happen. */
Py_CLEAR(def->m_base.m_copy);
}
PyObject *dict = PyModule_GetDict(mod);
if (dict == NULL) {
return -1;
}
def->m_base.m_copy = PyDict_Copy(dict);
if (def->m_base.m_copy == NULL) {
return -1;
}
}
dict = PyModule_GetDict(mod);
if (dict == NULL)

if (extensions == NULL) {
extensions = PyDict_New();
if (extensions == NULL) {
return -1;
}
}

PyObject *key = PyTuple_Pack(2, filename, name);
if (key == NULL) {
return -1;
def->m_base.m_copy = PyDict_Copy(dict);
if (def->m_base.m_copy == NULL)
}
int res = PyDict_SetItem(extensions, key, (PyObject *)def);
Py_DECREF(key);
if (res < 0) {
return -1;
}
}
key = PyTuple_Pack(2, filename, name);
if (key == NULL)
return -1;
res = PyDict_SetItem(extensions, key, (PyObject *)def);
Py_DECREF(key);
if (res < 0)
return -1;

return 0;
}

Expand Down Expand Up @@ -801,7 +813,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
}
Py_DECREF(mod);
}
if (_PyState_AddModule(mod, def) < 0) {
if (_PyState_AddModule(tstate, mod, def) < 0) {
PyMapping_DelItem(modules, name);
return NULL;
}
Expand Down
39 changes: 23 additions & 16 deletions Python/pystate.c