8000 gh-104310: Add importlib.util.allowing_all_extensions() by ericsnowcurrently · Pull Request #104311 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104310: Add importlib.util.allowing_all_extensions() #104311

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 1 commit
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
8000
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests.
  • Loading branch information
ericsnowcurrently committed May 8, 2023
commit 929f384b6d871f75d8bd60a3d4fad6e5de1fa26f
6 changes: 3 additions & 3 deletions Lib/importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ class allowing_all_extensions:
disable the check for compatible extension modules.
"""

def __init__(self, enabled):
self.enabled = enabled
def __init__(self, disable_check=True):
self.disable_check = disable_check

def __enter__(self):
self.old = _imp._override_multi_interp_extensions_check(self.override)
Expand All @@ -146,7 +146,7 @@ def __exit__(self, *args):

@property
def override(self):
return 1 if self.enabled else 0
return -1 if self.disable_check else 1


class _LazyModule(types.ModuleType):
Expand Down
121 changes: 121 additions & 0 deletions Lib/test/test_importlib/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,29 @@
import importlib.util
import os
import pathlib
import re
import string
import sys
from test import support
import textwrap
import types
import unittest
import unittest.mock
import warnings

try:
import _testsinglephase
except ImportError:
_testsinglephase = None
try:
import _testmultiphase
except ImportError:
_testmultiphase = None
try:
import _xxsubinterpreters as _interpreters
except ModuleNotFoundError:
_interpreters = None


class DecodeSourceBytesTests:

Expand Down Expand Up @@ -637,5 +652,111 @@ def test_magic_number(self):
self.assertEqual(EXPECTED_MAGIC_NUMBER, actual, msg)


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

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

def run_with_own_gil(self, script):
interpid = _interpreters.create(isolated=True)
try:
_interpreters.run_string(interpid, script)
except _interpreters.RunFailedError as exc:
if m := self.ERROR.match(str(exc)):
modname, = m.groups()
raise ImportError(modname)

def run_with_shared_gil(self, script):
interpid = _interpreters.create(isolated=False)
try:
_interpreters.run_string(interpid, script)
except _interpreters.RunFailedError as exc:
if m := self.ERROR.match(str(exc)):
modname, = m.groups()
raise ImportError(modname)

@unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
def test_single_phase_init_module(self):
script = textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions():
import _testsinglephase
''')
with self.subTest('check disabled, shared GIL'):
self.run_with_shared_gil(script)
with self.subTest('check disabled, per-interpreter GIL'):
self.run_with_own_gil(script)

script = textwrap.dedent(f'''
import importlib.util
with importlib.util.allowing_all_extensions(False):
import _testsinglephase
''')
with self.subTest('check enabled, shared GIL'):
with self.assertRaises(ImportError):
self.run_with_shared_gil(script)
with self.subTest('check enabled, per-interpreter GIL'):
with self.assertRaises(ImportError):
self.run_with_own_gil(script)

@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_incomplete_multi_phase_init_module(self):
prescript = textwrap.dedent(f'''
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import ExtensionFileLoader

name = '_test_shared_gil_only'
filename = {_testmultiphase.__file__!r}
loader = ExtensionFileLoader(name, filename)
spec = spec_from_loader(name, loader)

''')

script = prescript + textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions():
module = module_from_spec(spec)
loader.exec_module(module)
''')
with self.subTest('check disabled, shared GIL'):
self.run_with_shared_gil(script)
with self.subTest('check disabled, per-interpreter GIL'):
self.run_with_own_gil(script)

script = prescript + textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions(False):
module = module_from_spec(spec)
loader.exec_module(module)
''')
with self.subTest('check enabled, shared GIL'):
self.run_with_shared_gil(script)
with self.subTest('check enabled, per-interpreter GIL'):
with self.assertRaises(ImportError):
self.run_with_own_gil(script)

@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_complete_multi_phase_init_module(self):
script = textwrap.dedent('''
import importlib.util
with importlib.util.allowing_all_extensions():
import _testmultiphase
''')
with self.subTest('check disabled, shared GIL'):
self.run_with_shared_gil(script)
with self.subTest('check disabled, per-interpreter GIL'):
self.run_with_own_gil(script)

script = textwrap.dedent(f'''
import importlib.util
with importlib.util.allowing_all_extensions(False):
import _testmultiphase
''')
with self.subTest('check enabled, shared GIL'):
self.run_with_shared_gil(script)
with self.subTest('check enabled, per-interpreter GIL'):
self.run_with_own_gil(script)


if __name__ == '__main__':
unittest.main()
0