[3.9] bpo-44050: Extension modules can share state when they don't su… · python/cpython@52d9d3b · GitHub
Skip to content

Commit 52d9d3b

Browse files
ambvshihai1991
andauthored
[3.9] bpo-44050: Extension modules can share state when they don't support sub-interpreters. (GH-27794) (GH-28741)
(cherry picked from commit b9bb748) Co-authored-by: Hai Shi <shihai1992@gmail.com>
1 parent 950b324 commit 52d9d3b

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

Lib/test/test_capi.py

Lines changed: 31 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Extensions that indicate they use global state (by setting ``m_size`` to -1)
2+
can again be used in multiple interpreters. This reverts to behavior of
3+
Python 3.8.

Modules/_testmultiphase.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,30 @@ PyInit__testmultiphase_meth_state_access(PyObject *spec)
834834
return PyModuleDef_Init(&def_meth_state_access);
835835
}
836836

837+
static PyModuleDef def_module_state_shared = {
838+
PyModuleDef_HEAD_INIT,
839+
.m_name = "_test_module_state_shared",
840+
.m_doc = PyDoc_STR("Regression Test module for single-phase init."),
841+
.m_size = -1,
842+
};
843+
844+
PyMODINIT_FUNC
845+
PyInit__test_module_state_shared(PyObject *spec)
846+
{
847+
PyObject *module = PyModule_Create(&def_module_state_shared);
848+
if (module == NULL) {
849+
return NULL;
850+
}
851+
852+
Py_INCREF(PyExc_Exception);
853+
if (PyModule_AddObject(module, "Error", PyExc_Exception) < 0) {
854+
Py_DECREF(PyExc_Exception);
855+
Py_DECREF(module);
856+
return NULL;
857+
}
858+
return module;
859+
}
860+
837861

838862
/*** Helper for imp test ***/
839863

Python/import.c

Lines changed: 3 additions & 1 deletion

0 commit comments

Comments
 (0)