Skip to content

Commit 9ff3d32

Browse files
committed
gh-117649: Raise ImportError for unsupported modules in free-threaded build
The free-threaded build does not currently support the combination of single-phase init modules and legacy, non-isolated subinterpreters. Note that with isolated interpreters, single-phase init modules already trigger `ImportError`.
1 parent 24a2bd0 commit 9ff3d32

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

Lib/test/test_capi/test_misc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from test.support import threading_helper
2727
from test.support import warnings_helper
2828
from test.support import requires_limited_api
29+
from test.support import requires_gil_enabled
2930
from test.support.script_helper import assert_python_failure, assert_python_ok, run_python_until_end
3031
try:
3132
import _posixsubprocess
@@ -2070,6 +2071,7 @@ def test_configured_settings(self):
20702071

20712072
@unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
20722073
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
2074+
@requires_gil_enabled("gh-117649: test does not work in free-threaded build")
20732075
def test_overridden_setting_extensions_subinterp_check(self):
20742076
"""
20752077
PyInterpreterConfig.check_multi_interp_extensions can be overridden
@@ -2165,6 +2167,7 @@ def test_mutate_exception(self):
21652167
self.assertFalse(hasattr(binascii.Error, "foobar"))
21662168

21672169
@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
2170+
@requires_gil_enabled("gh-117649: test does not work in free-threaded build")
21682171
def test_module_state_shared_in_global(self):
21692172
"""
21702173
bpo-44050: Extension module state should be shared between interpreters

Lib/test/test_import/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
from test.support import os_helper
3131
from test.support import (
3232
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_apple_mobile, is_emscripten,
33-
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS)
33+
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS,
34+
requires_gil_enabled)
3435
from test.support.import_helper import (
3536
forget, make_legacy_pyc, unlink, unload, ready_to_import,
3637
DirsOnSysPath, CleanImport, import_module)
@@ -158,6 +159,9 @@ def meth(self, _meth=meth):
158159
finally:
159160
restore__testsinglephase()
160161
meth = cpython_only(meth)
162+
# gh-117649: free-threaded build does not currently support single-phase
163+
# init modules in subinterpreters.
164+
meth = requires_gil_enabled(meth)
161165
return unittest.skipIf(_testsinglephase is None,
162166
'test requires _testsinglephase module')(meth)
163167

Lib/test/test_importlib/test_util.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import re
1212
import string
1313
import sys
14+
import sysconfig
1415
from test import support
1516
import textwrap
1617
import types
@@ -688,10 +689,20 @@ def test_single_phase_init_module(self):
688689
with _incompatible_extension_module_restrictions(disable_check=True):
689690
import _testsinglephase
690691
''')
691-
with self.subTest('check disabled, shared GIL'):
692-
self.run_with_shared_gil(script)
693-
with self.subTest('check disabled, per-interpreter GIL'):
694-
self.run_with_own_gil(script)
692+
if not sysconfig.get_config_var('Py_GIL_DISABLED'):
693+
with self.subTest('check disabled, shared GIL'):
694+
self.run_with_shared_gil(script)
695+
with self.subTest('check disabled, per-interpreter GIL'):
696+
self.run_with_own_gil(script)
697+
else:
698+
# gh-117649: Py_GIL_DISABLED builds do not support legacy
699+
# single-phase init extensions within subinterpreters.
700+
with self.subTest('check disabled, shared GIL'):
701+
with self.assertRaises(ImportError):
702+
self.run_with_shared_gil(script)
703+
with self.subTest('check disabled, per-interpreter GIL'):
704+
with self.assertRaises(ImportError):
705+
self.run_with_own_gil(script)
695706

696707
script = textwrap.dedent(f'''
697708
from importlib.util import _incompatible_extension_module_restrictions

Python/import.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,19 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
12441244
return NULL;
12451245
}
12461246

1247+
/* gh-117649: The free-threaded build does not currently support
1248+
single-phase init modules in subinterpreters. */
1249+
#ifdef Py_GIL_DISABLED
1250+
if (def->m_size == -1 && !_Py_IsMainInterpreter(tstate->interp)) {
1251+
return PyErr_Format(
1252+
PyExc_ImportError,
1253+
"module %s does not support the combination of free-threading "
1254+
"and subinterpreters",
1255+
name_buf);
1256+
return NULL;
1257+
}
1258+
#endif
1259+
12471260
PyObject *mod, *mdict;
12481261
PyObject *modules = MODULES(tstate->interp);
12491262

0 commit comments

Comments
 (0)