Skip to content

Commit 72ba954

Browse files
authored
gh-161617: Fix test_clinic, test_tools, test_generated_cases with lazy imports (#161618)
1 parent 9f38636 commit 72ba954

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎Lib/test/lazy_imports_all_exclude.txt‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
test.test_inspect.test_inspect
2020
test___all__
2121
test__interpreters
22-
test_clinic
2322
test_crossinterp
2423
test_datetime
25-
test_generated_cases
2624
test_import
2725
test_importlib
2826
test_json
@@ -32,6 +30,5 @@ test_profiling
3230
test_pyrepl
3331
test_subprocess
3432
test_symtable
35-
test_tools
3633
test_type_annotations
3734
test_unittest

‎Lib/test/support/import_helper.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ def frozen_modules(enabled=True):
119119
_imp._override_frozen_modules_for_tests(0)
120120

121121

122+
@contextlib.contextmanager
123+
def force_eager_imports():
124+
"""Force eager imports inside the context manager."""
125+
original_lazy_imports_filter = sys.get_lazy_imports_filter()
126+
sys.set_lazy_imports_filter(
127+
# Tools must be imported eagerly under this context manager:
128+
lambda importing, imported, fromlist: False
129+
)
130+
try:
131+
yield
132+
finally:
133+
sys.set_lazy_imports_filter(original_lazy_imports_filter)
134+
135+
122136
@contextlib.contextmanager
123137
def multi_interp_extensions_check(enabled=True):
124138
"""Force legacy modules to be allowed in subinterpreters (or not).

‎Lib/test/test_tools/__init__.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def skip_if_missing(tool=None):
3232
@contextlib.contextmanager
3333
def imports_under_tool(name, *subdirs):
3434
tooldir = os.path.join(toolsdir, name, *subdirs)
35-
with import_helper.DirsOnSysPath(tooldir) as cm:
35+
with (
36+
import_helper.force_eager_imports(),
37+
import_helper.DirsOnSysPath(tooldir) as cm,
38+
):
3639
yield cm
3740

3841
def import_tool(toolname):

0 commit comments

Comments
 (0)