8000 gh-99113: Add a check for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED by ericsnowcurrently · Pull Request #104206 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99113: Add a check for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED #104206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 20 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,26 @@ def test_multi_init_extension_non_isolated_compat(self):
with self.subTest(f'{modname}: not strict'):
self.check_compatible_here(modname, filename, strict=False)

@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_multi_init_extension_per_interpreter_gil_compat(self):
modname = '_test_shared_gil_only'
filename = _testmultiphase.__file__
loader = ExtensionFileLoader(modname, filename)
spec = importlib.util.spec_from_loader(modname, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
sys.modules[modname] = module

require_extension(module)
with self.subTest(f'{modname}: isolated, strict'):
self.check_incompatible_here(modname, filename, isolated=True)
with self.subTest(f'{modname}: not isolated, strict'):
self.check_compatible_here(modname, filename,
strict=True, isolated=False)
with self.subTest(f'{modname}: not isolated, not strict'):
self.check_compatible_here(modname, filename,
strict=False, isolated=False)

def test_python_compat(self):
module = 'threading'
require_pure_python(module)
Expand Down
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Multi-phase init extension modules may now indicate that they support
running in subinterpreters that have their own GIL. This is done by using
``Py_MOD_PER_INTERPRETER_GIL_SUPPORTED`` as the value for the
``Py_mod_multiple_interpreters`` module def slot. Otherwise the module, by
default, cannot be imported in such subinterpreters. (This does not affect
the main interpreter or subinterpreters that do not have their own GIL.) In
addition to the isolation that multi-phase init already normally requires,
support for per-interpreter GIL involves one additional constraint:
thread-safety. If the module has external (linked) dependencies and those
libraries have any state that isn't thread-safe then the module must do the
additional work to add thread-safety. This should be an uncommon case.
19 changes: 18 additions & 1 deletion Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ PyInit__test_module_state_shared(void)
}


/* multiple interpreters supports */
/* multiple interpreters support */

static PyModuleDef_Slot non_isolated_slots[] = {
{Py_mod_exec, execfunc},
Expand All @@ -909,3 +909,20 @@ PyInit__test_non_isolated(void)
{
return PyModuleDef_Init(&non_isolated_def);
}


static PyModuleDef_Slot shared_gil_only_slots[] = {
{Py_mod_exec, execfunc},
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED},
{0, NULL},
};

static PyModuleDef shared_gil_only_def = TEST_MODULE_DEF("_test_shared_gil_only",
shared_gil_only_slots,
testexport_methods);

PyMODINIT_FUNC
PyInit__test_shared_gil_only(void)
{
return PyModuleDef_Init(&shared_gil_only_def);
}
8 changes: 7 additions & 1 deletion Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,13 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
goto error;
}
}
// XXX Do a similar check once we have PyInterpreterState.ceval.own_gil.
else if (multiple_interpreters != Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
&& interp->ceval.own_gil
&& !_Py_IsMainInterpreter(interp)
&& _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
{
goto error;
}

if (create) {
m = create(spec, def);
Expand Down
0