FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44050: Extension modules can share state when they don't support sub-interpreters. by shihai1991 · Pull Request #27794 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 31 additions & 0 deletions Lib/test/test_capi.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,37 @@ def test_mutate_exception(self):

self.assertFalse(hasattr(binascii.Error, "foobar"))

def test_module_state_shared_in_global(self):
"""
bpo-44050: Extension module state should be shared between interpreters
when it doesn't support sub-interpreters.
"""
r, w = os.pipe()
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)

script = textwrap.dedent(f"""
import importlib.machinery
import importlib.util
import os

fullname = '_test_module_state_shared'
origin = importlib.util.find_spec('_testmultiphase').origin
loader = importlib.machinery.ExtensionFileLoader(fullname, origin)
spec = importlib.util.spec_from_loader(fullname, loader)
module = importlib.util.module_from_spec(spec)
attr_id = str(id(module.Error)).encode()

os.write({w}, attr_id)
""")
exec(script)
main_attr_id = os.read(r, 100)

ret = support.run_in_subinterp(script)
self.assertEqual(ret, 0)
subinterp_attr_id = os.read(r, 100)
self.assertEqual(main_attr_id, subinterp_attr_id)


class TestThreadState(unittest.TestCase):

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Extensions that indicate they use global state (by setting ``m_size`` to -1)
can again be used in multiple interpreters. This reverts to behavior of
Python 3.8.
22 changes: 22 additions & 0 deletions Modules/_testmultiphase.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,28 @@ PyInit__testmultiphase_meth_state_access(PyObject *spec)
return PyModuleDef_Init(&def_meth_state_access);
}

static PyModuleDef def_module_state_shared = {
PyModuleDef_HEAD_INIT,
.m_name = "_test_module_state_shared",
.m_doc = PyDoc_STR("Regression Test module for single-phase init."),
.m_size = -1,
};

PyMODINIT_FUNC
PyInit__test_module_state_shared(PyObject *spec)
{
PyObject *module = PyModule_Create(&def_module_state_shared);
if (module == NULL) {
return NULL;
}

if (PyModule_AddObjectRef(module, "Error", PyExc_Exception) < 0) {
Py_DECREF(module);
return NULL;
}
return module;
}


/*** Helper for imp test ***/

Expand Down
4 changes: 3 additions & 1 deletion Python/import.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
return -1;
}

if (_Py_IsMainInterpreter(tstate->interp)) {
// bpo-44050: Extensions and def->m_base.m_copy can be updated
// when the extension module doesn't support sub-interpreters.
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
if (def->m_size == -1) {
if (def->m_base.m_copy) {
/* Somebody already imported the module,
Expand Down

Back | FazBrowse Home | New Git URL