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

gh-104310: Rename the New Function in importlib.util #105255

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
26 changes: 21 additions & 5 deletions Lib/importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,24 @@ def find_spec(name, package=None):
# is imported by runpy, which means we want to avoid any unnecessary
# dependencies. Thus we use a class.

class allowing_all_extensions:
"""A context manager that lets users skip the compatibility check.
class _incompatible_extension_module_restrictions:
brettcannon marked this conversation as resolved.
Show resolved Hide resolved
"""A context manager that can temporarily skip the compatibility check.

NOTE: This function is meant to accommodate an unusual case; one
which is likely to eventually go away. There's is a pretty good
chance this is not what you were looking for.

WARNING: Using this function to disable the check can lead to
unexpected behavior and even crashes. It should only be used during
extension module development.

If "disable_check" is True then the compatibility check will not
happen while the context manager is active. Otherwise the check
*will* happen.

Normally, extensions that do not support multiple interpreters
may not be imported in a subinterpreter. That implies modules
that do not implement multi-phase init.
that do not implement multi-phase init or that explicitly of out.

Likewise for modules import in a subinterpeter with its own GIL
when the extension does not support a per-interpreter GIL. This
Expand All @@ -130,10 +142,14 @@ class allowing_all_extensions:

In both cases, this context manager may be used to temporarily
disable the check for compatible extension modules.

You can get the same effect as this function by implementing the
basic interface of multi-phase init (PEP 489) and lying about
support for mulitple interpreters (or per-interpreter GIL).
"""

def __init__(self, disable_check=True):
self.disable_check = disable_check
def __init__(self, disable_check):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the keyword, _incompatible_extension_module_restrictions(True) reads to me like incompatible module restrictions are turned on. I would either make the parameter keyword-only or invert the meaning (i.e., enable_check).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self.disable_check = bool(disable_check)

def __enter__(self):
self.old = _imp._override_multi_interp_extensions_check(self.override)
Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_importlib/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def test_magic_number(self):


@unittest.skipIf(_interpreters is None, 'subinterpreters required')
class AllowingAllExtensionsTests(unittest.TestCase):
class IncompatibleExtensionModuleRestrictionsTests(unittest.TestCase):

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

Expand All @@ -679,7 +679,7 @@ def run_with_shared_gil(self, script):
def test_single_phase_init_module(self):
script = textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions():
with importlib.util._incompatible_extension_module_restrictions(True):
import _testsinglephase
''')
with self.subTest('check disabled, shared GIL'):
Expand All @@ -689,7 +689,7 @@ def test_single_phase_init_module(self):

script = textwrap.dedent(f'''
import importlib.util
with importlib.util.allowing_all_extensions(False):
with importlib.util._incompatible_extension_module_restrictions(False):
import _testsinglephase
''')
with self.subTest('check enabled, shared GIL'):
Expand All @@ -714,7 +714,7 @@ def test_incomplete_multi_phase_init_module(self):

script = prescript + textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions():
with importlib.util._incompatible_extension_module_restrictions(True):
module = module_from_spec(spec)
loader.exec_module(module)
''')
Expand All @@ -725,7 +725,7 @@ def test_incomplete_multi_phase_init_module(self):

script = prescript + textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions(False):
with importlib.util._incompatible_extension_module_restrictions(False):
module = module_from_spec(spec)
loader.exec_module(module)
''')
Expand All @@ -739,7 +739,7 @@ def test_incomplete_multi_phase_init_module(self):
def test_complete_multi_phase_init_module(self):
script = textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions():
with importlib.util._incompatible_extension_module_restrictions(True):
import _testmultiphase
''')
with self.subTest('check disabled, shared GIL'):
Expand All @@ -749,7 +749,7 @@ def test_complete_multi_phase_init_module(self):

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