bpo-38858: _PyImport_FixupExtensionObject() handles subinterpreters (… · python/cpython@82c83bd (original) (raw)
`@@ -695,50 +695,62 @@ int
`
695
695
`_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
`
696
696
`PyObject *filename, PyObject *modules)
`
697
697
`{
`
698
``
`-
PyObject *dict, *key;
`
699
``
`-
struct PyModuleDef *def;
`
700
``
`-
int res;
`
701
``
`-
if (extensions == NULL) {
`
702
``
`-
extensions = PyDict_New();
`
703
``
`-
if (extensions == NULL)
`
704
``
`-
return -1;
`
705
``
`-
}
`
706
698
`if (mod == NULL || !PyModule_Check(mod)) {
`
707
699
`PyErr_BadInternalCall();
`
708
700
`return -1;
`
709
701
` }
`
710
``
`-
def = PyModule_GetDef(mod);
`
``
702
+
``
703
`+
struct PyModuleDef *def = PyModule_GetDef(mod);
`
711
704
`if (!def) {
`
712
705
`PyErr_BadInternalCall();
`
713
706
`return -1;
`
714
707
` }
`
715
``
`-
if (PyObject_SetItem(modules, name, mod) < 0)
`
``
708
+
``
709
`+
PyThreadState *tstate = _PyThreadState_GET();
`
``
710
`+
if (PyObject_SetItem(modules, name, mod) < 0) {
`
716
711
`return -1;
`
717
``
`-
if (_PyState_AddModule(mod, def) < 0) {
`
``
712
`+
}
`
``
713
`+
if (_PyState_AddModule(tstate, mod, def) < 0) {
`
718
714
`PyMapping_DelItem(modules, name);
`
719
715
`return -1;
`
720
716
` }
`
721
``
`-
if (def->m_size == -1) {
`
722
``
`-
if (def->m_base.m_copy) {
`
723
``
`-
/* Somebody already imported the module,
`
724
``
`-
likely under a different name.
`
725
``
`-
XXX this should really not happen. */
`
726
``
`-
Py_CLEAR(def->m_base.m_copy);
`
``
717
+
``
718
`+
if (_Py_IsMainInterpreter(tstate)) {
`
``
719
`+
if (def->m_size == -1) {
`
``
720
`+
if (def->m_base.m_copy) {
`
``
721
`+
/* Somebody already imported the module,
`
``
722
`+
likely under a different name.
`
``
723
`+
XXX this should really not happen. */
`
``
724
`+
Py_CLEAR(def->m_base.m_copy);
`
``
725
`+
}
`
``
726
`+
PyObject *dict = PyModule_GetDict(mod);
`
``
727
`+
if (dict == NULL) {
`
``
728
`+
return -1;
`
``
729
`+
}
`
``
730
`+
def->m_base.m_copy = PyDict_Copy(dict);
`
``
731
`+
if (def->m_base.m_copy == NULL) {
`
``
732
`+
return -1;
`
``
733
`+
}
`
727
734
` }
`
728
``
`-
dict = PyModule_GetDict(mod);
`
729
``
`-
if (dict == NULL)
`
``
735
+
``
736
`+
if (extensions == NULL) {
`
``
737
`+
extensions = PyDict_New();
`
``
738
`+
if (extensions == NULL) {
`
``
739
`+
return -1;
`
``
740
`+
}
`
``
741
`+
}
`
``
742
+
``
743
`+
PyObject *key = PyTuple_Pack(2, filename, name);
`
``
744
`+
if (key == NULL) {
`
730
745
`return -1;
`
731
``
`-
def->m_base.m_copy = PyDict_Copy(dict);
`
732
``
`-
if (def->m_base.m_copy == NULL)
`
``
746
`+
}
`
``
747
`+
int res = PyDict_SetItem(extensions, key, (PyObject *)def);
`
``
748
`+
Py_DECREF(key);
`
``
749
`+
if (res < 0) {
`
733
750
`return -1;
`
``
751
`+
}
`
734
752
` }
`
735
``
`-
key = PyTuple_Pack(2, filename, name);
`
736
``
`-
if (key == NULL)
`
737
``
`-
return -1;
`
738
``
`-
res = PyDict_SetItem(extensions, key, (PyObject *)def);
`
739
``
`-
Py_DECREF(key);
`
740
``
`-
if (res < 0)
`
741
``
`-
return -1;
`
``
753
+
742
754
`return 0;
`
743
755
`}
`
744
756
``
`@@ -801,7 +813,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
`
801
813
` }
`
802
814
`Py_DECREF(mod);
`
803
815
` }
`
804
``
`-
if (_PyState_AddModule(mod, def) < 0) {
`
``
816
`+
if (_PyState_AddModule(tstate, mod, def) < 0) {
`
805
817
`PyMapping_DelItem(modules, name);
`
806
818
`return NULL;
`
807
819
` }
`