Skip to content

Commit 34c63b8

Browse files
gh-104310: Rename the New Function in importlib.util (gh-105255)
The original name wasn't as clear as it could have been. This change includes the following: * rename the function * change the default value for "disable_check" to False * add clues to the docstring that folks should probably not use the function --------- Co-authored-by: Kirill Podoprigora <[email protected]>
1 parent 7799c8e commit 34c63b8

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

Lib/importlib/util.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,24 @@ def find_spec(name, package=None):
116116
# is imported by runpy, which means we want to avoid any unnecessary
117117
# dependencies. Thus we use a class.
118118

119-
class allowing_all_extensions:
120-
"""A context manager that lets users skip the compatibility check.
119+
class _incompatible_extension_module_restrictions:
120+
"""A context manager that can temporarily skip the compatibility check.
121+
122+
NOTE: This function is meant to accommodate an unusual case; one
123+
which is likely to eventually go away. There's is a pretty good
124+
chance this is not what you were looking for.
125+
126+
WARNING: Using this function to disable the check can lead to
127+
unexpected behavior and even crashes. It should only be used during
128+
extension module development.
129+
130+
If "disable_check" is True then the compatibility check will not
131+
happen while the context manager is active. Otherwise the check
132+
*will* happen.
121133
122134
Normally, extensions that do not support multiple interpreters
123135
may not be imported in a subinterpreter. That implies modules
124-
that do not implement multi-phase init.
136+
that do not implement multi-phase init or that explicitly of out.
125137
126138
Likewise for modules import in a subinterpeter with its own GIL
127139
when the extension does not support a per-interpreter GIL. This
@@ -130,10 +142,14 @@ class allowing_all_extensions:
130142
131143
In both cases, this context manager may be used to temporarily
132144
disable the check for compatible extension modules.
145+
146+
You can get the same effect as this function by implementing the
147+
basic interface of multi-phase init (PEP 489) and lying about
148+
support for mulitple interpreters (or per-interpreter GIL).
133149
"""
134150

135-
def __init__(self, disable_check=True):
136-
self.disable_check = disable_check
151+
def __init__(self, *, disable_check):
152+
self.disable_check = bool(disable_check)
137153

138154
def __enter__(self):
139155
self.old = _imp._override_multi_interp_extensions_check(self.override)

Lib/test/test_importlib/test_util.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def test_magic_number(self):
653653

654654

655655
@unittest.skipIf(_interpreters is None, 'subinterpreters required')
656-
class AllowingAllExtensionsTests(unittest.TestCase):
656+
class IncompatibleExtensionModuleRestrictionsTests(unittest.TestCase):
657657

658658
ERROR = re.compile("^<class 'ImportError'>: module (.*) does not support loading in subinterpreters")
659659

@@ -678,8 +678,8 @@ def run_with_shared_gil(self, script):
678678
@unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
679679
def test_single_phase_init_module(self):
680680
script = textwrap.dedent('''
681-
import importlib.util
682-
with importlib.util.allowing_all_extensions():
681+
from importlib.util import _incompatible_extension_module_restrictions
682+
with _incompatible_extension_module_restrictions(disable_check=True):
683683
import _testsinglephase
684684
''')
685685
with self.subTest('check disabled, shared GIL'):
@@ -688,8 +688,8 @@ def test_single_phase_init_module(self):
688688
self.run_with_own_gil(script)
689689

690690
script = textwrap.dedent(f'''
691-
import importlib.util
692-
with importlib.util.allowing_all_extensions(False):
691+
from importlib.util import _incompatible_extension_module_restrictions
692+
with _incompatible_extension_module_restrictions(disable_check=False):
693693
import _testsinglephase
694694
''')
695695
with self.subTest('check enabled, shared GIL'):
@@ -713,8 +713,8 @@ def test_incomplete_multi_phase_init_module(self):
713713
''')
714714

715715
script = prescript + textwrap.dedent('''
716-
import importlib.util
717-
with importlib.util.allowing_all_extensions():
716+
from importlib.util import _incompatible_extension_module_restrictions
717+
with _incompatible_extension_module_restrictions(disable_check=True):
718718
module = module_from_spec(spec)
719719
loader.exec_module(module)
720720
''')
@@ -724,8 +724,8 @@ def test_incomplete_multi_phase_init_module(self):
724724
self.run_with_own_gil(script)
725725

726726
script = prescript + textwrap.dedent('''
727-
import importlib.util
728-
with importlib.util.allowing_all_extensions(False):
727+
from importlib.util import _incompatible_extension_module_restrictions
728+
with _incompatible_extension_module_restrictions(disable_check=False):
729729
module = module_from_spec(spec)
730730
loader.exec_module(module)
731731
''')
@@ -738,8 +738,8 @@ def test_incomplete_multi_phase_init_module(self):
738738
@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
739739
def test_complete_multi_phase_init_module(self):
740740
script = textwrap.dedent('''
741-
import importlib.util
742-
with importlib.util.allowing_all_extensions():
741+
from importlib.util import _incompatible_extension_module_restrictions
742+
with _incompatible_extension_module_restrictions(disable_check=True):
743743
import _testmultiphase
744744
''')
745745
with self.subTest('check disabled, shared GIL'):
@@ -748,8 +748,8 @@ def test_complete_multi_phase_init_module(self):
748748
self.run_with_own_gil(script)
749749

750750
script = textwrap.dedent(f'''
751-
import importlib.util
752-
with importlib.util.allowing_all_extensions(False):
751+
from importlib.util import _incompatible_extension_module_restrictions
752+
with _incompatible_extension_module_restrictions(disable_check=False):
753753
import _testmultiphase
754754
''')
755755
with self.subTest('check enabled, shared GIL'):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
In the beta 1 release we added a utility function for extension module
2+
authors, to use when testing their module for support in multiple
3+
interpreters or under a per-interpreter GIL. The name of that function has
4+
changed from ``allowing_all_extensions`` to
5+
``_incompatible_extension_module_restrictions``. The default for the
6+
"disable_check" argument has change from ``True`` to ``False``, to better
7+
match the new function name.

0 commit comments

Comments
 (0)