Skip to content
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

[3.12] gh-129405: Fix doc for Py_mod_multiple_interpreters default, and add test (GH-129406) #130510

Open
wants to merge 1 commit into
base: 3.12
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Doc/c-api/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ The available slot types are:
in one module definition.

If ``Py_mod_multiple_interpreters`` is not specified, the import
machinery defaults to ``Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED``.
machinery defaults to ``Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED``.

.. versionadded:: 3.12

Expand Down
47 changes: 30 additions & 17 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,7 @@ def test_single_init_extension_compat(self):

@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_multi_init_extension_compat(self):
# Module with Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
module = '_testmultiphase'
require_extension(module)
with self.subTest(f'{module}: not strict'):
Expand All @@ -1911,6 +1912,8 @@ def test_multi_init_extension_compat(self):

@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_multi_init_extension_non_isolated_compat(self):
# Module with Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED
# and Py_MOD_GIL_NOT_USED
modname = '_test_non_isolated'
filename = _testmultiphase.__file__
loader = ExtensionFileLoader(modname, filename)
Expand All @@ -1929,23 +1932,33 @@ def test_multi_init_extension_non_isolated_compat(self):

@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)
# _test_shared_gil_only:
# Explicit Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED (default)
# and Py_MOD_GIL_NOT_USED
# _test_no_multiple_interpreter_slot:
# No Py_mod_multiple_interpreters slot
# and Py_MOD_GIL_NOT_USED
for modname in ('_test_shared_gil_only',
'_test_no_multiple_interpreter_slot'):
with self.subTest(modname=modname):

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'
Expand Down
17 changes: 17 additions & 0 deletions Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -967,3 +967,20 @@ PyInit__test_shared_gil_only(void)
{
return PyModuleDef_Init(&shared_gil_only_def);
}


static PyModuleDef_Slot no_multiple_interpreter_slot_slots[] = {
{Py_mod_exec, execfunc},
{0, NULL},
};

static PyModuleDef no_multiple_interpreter_slot_def = TEST_MODULE_DEF(
"_test_no_multiple_interpreter_slot",
no_multiple_interpreter_slot_slots,
testexport_methods);

PyMODINIT_FUNC
PyInit__test_no_multiple_interpreter_slot(void)
{
return PyModuleDef_Init(&no_multiple_interpreter_slot_def);
}
Loading