Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions Lib/test/lazy_imports_all_exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,6 +30,5 @@ test_profiling
test_pyrepl
test_subprocess
test_symtable
test_tools
test_type_annotations
test_unittest
14 changes: 14 additions & 0 deletions Lib/test/support/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def skip_if_missing(tool=None):
@contextlib.contextmanager
def imports_under_tool(name, *subdirs):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh wait, does it not make sense to do it here instead?

@sobolevn sobolevn Nov 8, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think so, it might be required for other places that use DirsOnSysPath as well, since it is a broader API. We can alternatevly make a separate API called with forced_eager_imports(): and use it here. WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I think it is a better and more compsable API :)

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):
Expand Down
Loading