Skip to content

Commit a2c9395

Browse files
committed
Add test helper logic to allow for additional commands in lazy import tests
1 parent bfe6f9f commit a2c9395

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/support/import_helper.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def ready_to_import(name=None, source=""):
325325
sys.modules.pop(name, None)
326326

327327

328-
def ensure_lazy_imports(imported_module, modules_to_block):
328+
def ensure_lazy_imports(imported_module, modules_to_block, *, additional_code=None):
329329
"""Test that when imported_module is imported, none of the modules in
330330
modules_to_block are imported as a side effect."""
331331
modules_to_block = frozenset(modules_to_block)
@@ -343,6 +343,16 @@ def ensure_lazy_imports(imported_module, modules_to_block):
343343
raise AssertionError(f'unexpectedly imported after importing {imported_module}: {{after}}')
344344
"""
345345
)
346+
if additional_code:
347+
script += additional_code
348+
script += textwrap.dedent(
349+
f"""
350+
if unexpected := modules_to_block & sys.modules.keys():
351+
after = ", ".join(unexpected)
352+
raise AssertionError(f'unexpectedly imported after additional code: {{after}}')
353+
"""
354+
)
355+
346356
from .script_helper import assert_python_ok
347357
assert_python_ok("-S", "-c", script)
348358

0 commit comments

Comments
 (0)