8000 gh-101659: Clean Up the General Import Tests for Subinterpreters by ericsnowcurrently · Pull Request #103151 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-101659: Clean Up the General Import Tests for Subinterpreters #103151

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
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
Diff view
Diff view
Prev Previous commit
Ensure we are testing against the right type of extension.
  • Loading branch information
ericsnowcurrently committed Mar 31, 2023
commit 9c8d48026af7e81d6227fcb3afd0fca272715e45
54 changes: 52 additions & 2 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import glob
import importlib.util
from importlib._bootstrap_external import _get_sourcefile
from importlib.machinery import (
BuiltinImporter, ExtensionFileLoader, FrozenImporter, SourceFileLoader,
)
import marshal
import os
import py_compile
Expand Down Expand Up @@ -44,6 +47,49 @@
sys.dont_write_bytecode,
"test meaningful only when writing bytecode")


def _require_loader(module, loader, skip):
if isinstance(module, str):
module = __import__(module)

MODULE_KINDS = {
BuiltinImporter: 'built-in',
ExtensionFileLoader: 'extension',
FrozenImporter: 'frozen',
SourceFileLoader: 'pure Python',
}

expected = loader
assert isinstance(expected, type), expected
expected = MODULE_KINDS[expected]

actual = module.__spec__.loader
if not isinstance(actual, type):
actual = type(actual)
actual = MODULE_KINDS[actual]

if actual != expected:
err = f'expected module to be {expected}, got {module.__spec__}'
if skip:
raise unittest.SkipTest(err)
raise Exception(err)
return module

def require_builtin(module, *, skip=False):
module = _require_loader(module, BuiltinImporter, skip)
assert module.__spec__.origin == 'built-in', module.__spec__

def require_extension(module, *, skip=False):
_require_loader(module, ExtensionFileLoader, skip)

def require_frozen(module, *, skip=True):
module = _require_loader(module, FrozenImporter, skip)
assert module.__spec__.origin == 'frozen', module.__spec__

def require_pure_python(module, *, skip=False):
_require_loader(module, SourceFileLoader, skip)


def remove_files(name):
for f in (name + ".py",
name + ".pyc",
Expand Down Expand Up @@ -1541,6 +1587,7 @@ def test_builtin_compat(self):
# For now we avoid using sys or builtins
# since they still don't implement multi-phase init.
module = '_imp'
require_builtin(module)
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
Expand All @@ -1549,6 +1596,7 @@ def test_builtin_compat(self):
@cpython_only
def test_frozen_compat(self):
module = '_frozen_importlib'
require_frozen(module, skip=True)
if __import__(module).__spec__.origin != 'frozen':
raise unittest.SkipTest(f'{module} is unexpectedly not frozen')
with self.subTest(f'{module}: not strict'):
Expand All @@ -1559,6 +1607,7 @@ def test_frozen_compat(self):
@unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
def test_single_init_extension_compat(self):
module = '_testsinglephase'
require_extension(module)
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
Expand All @@ -1569,6 +1618,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 = '_testmultiphase'
require_extension(module)
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
Expand All @@ -1578,8 +1628,7 @@ def test_multi_init_extension_compat(self):

def test_python_compat(self):
module = 'threading'
if __import__(module).__spec__.origin == 'frozen':
raise unittest.SkipTest(f'{module} is unexpectedly frozen')
require_pure_python(module)
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
Expand All @@ -1590,6 +1639,7 @@ def test_python_compat(self):
@unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
def test_singlephase_check_with_setting_and_override(self):
module = '_testsinglephase'
require_extension(module)

def check_compatible(setting, override):
out = self.run_here(
Expand Down
0