diff --git a/Lib/test/lazy_imports_all_exclude.txt b/Lib/test/lazy_imports_all_exclude.txt index 864d723fe56b11a..e5519323e88e38a 100644 --- a/Lib/test/lazy_imports_all_exclude.txt +++ b/Lib/test/lazy_imports_all_exclude.txt @@ -19,10 +19,8 @@ test.test_inspect.test_inspect test___all__ test__interpreters -test_clinic test_crossinterp test_datetime -test_generated_cases test_import test_importlib test_json @@ -32,6 +30,5 @@ test_profiling test_pyrepl test_subprocess test_symtable -test_tools test_type_annotations test_unittest diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index e8a3d176ad6943f..962f65d8dcd14a2 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -119,6 +119,20 @@ def frozen_modules(enabled=True): _imp._override_frozen_modules_for_tests(0) +@contextlib.contextmanager +def force_eager_imports(): + """Force eager imports inside the context manager.""" + original_lazy_imports_filter = sys.get_lazy_imports_filter() + sys.set_lazy_imports_filter( + # Tools must be imported eagerly under this context manager: + lambda importing, imported, fromlist: False + ) + try: + yield + finally: + sys.set_lazy_imports_filter(original_lazy_imports_filter) + + @contextlib.contextmanager def multi_interp_extensions_check(enabled=True): """Force legacy modules to be allowed in subinterpreters (or not). diff --git a/Lib/test/test_tools/__init__.py b/Lib/test/test_tools/__init__.py index c4395c7c0ad0c98..310bf48c2ef03a1 100644 --- a/Lib/test/test_tools/__init__.py +++ b/Lib/test/test_tools/__init__.py @@ -32,7 +32,10 @@ def skip_if_missing(tool=None): @contextlib.contextmanager def imports_under_tool(name, *subdirs): tooldir = os.path.join(toolsdir, name, *subdirs) - with import_helper.DirsOnSysPath(tooldir) as cm: + with ( + import_helper.force_eager_imports(), + import_helper.DirsOnSysPath(tooldir) as cm, + ): yield cm def import_tool(toolname):