Skip to content

Commit 0e66382

Browse files
committed
pythongh-149321: Fix stdlib imports with lazy imports disabled
1 parent 68fe899 commit 0e66382

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

Lib/ast.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
:license: Python License.
2222
"""
2323
from _ast import *
24-
lazy from _colorize import can_colorize, get_theme
2524

2625

2726
def parse(source, filename='<unknown>', mode='exec', *,
@@ -142,6 +141,8 @@ def dump(
142141
If show_empty is False, then empty lists and fields that are None
143142
will be omitted from the output for better readability.
144143
"""
144+
from _colorize import get_theme
145+
145146
t = get_theme(force_color=color, force_no_color=not color).ast
146147

147148
def _format(node, level=0):
@@ -708,6 +709,7 @@ def main(args=None):
708709

709710
tree = parse(source, name, args.mode, type_comments=args.no_type_comments,
710711
feature_version=feature_version, optimize=args.optimize)
712+
from _colorize import can_colorize
711713
print(dump(tree, include_attributes=args.include_attributes,
712714
color=can_colorize(file=sys.stdout),
713715
indent=args.indent, show_empty=args.show_empty))

Lib/dataclasses.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import abc
77
from reprlib import recursive_repr
88
lazy import copy
9-
lazy import inspect
109
lazy import re
1110

1211

@@ -992,6 +991,7 @@ def __get__(self, _obj, cls):
992991
try:
993992
# In some cases fetching a signature is not possible.
994993
# But, we surely should not fail in this case.
994+
import inspect
995995
text_sig = str(inspect.signature(
996996
cls,
997997
annotation_format=annotationlib.Format.FORWARDREF,
@@ -1401,6 +1401,7 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
14011401

14021402
# If this is a wrapped function, unwrap it.
14031403
if not isinstance(member, type) and hasattr(member, '__wrapped__'):
1404+
import inspect
14041405
member = inspect.unwrap(member)
14051406

14061407
if isinstance(member, types.FunctionType):

Lib/test/test_lazy_import/__init__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,50 @@ def test_cli_lazy_imports_none_forces_all_imports_eager(self):
10341034
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
10351035
self.assertIn("EAGER", result.stdout)
10361036

1037+
@support.requires_resource("cpu")
1038+
def test_cli_lazy_imports_modes_import_stdlib_modules(self):
1039+
"""-X lazy_imports modes should import available stdlib modules."""
1040+
# Do not smoke-test modules with intentional import-time effects.
1041+
import_side_effect_modules = {"antigravity", "this"}
1042+
importable = []
1043+
1044+
for module in sorted(sys.stdlib_module_names):
1045+
if module in import_side_effect_modules:
1046+
continue
1047+
1048+
with self.subTest(module=module):
1049+
code = f"import {module}; print({module})"
1050+
baseline = subprocess.run(
1051+
[sys.executable, "-I", "-c", code],
1052+
capture_output=True,
1053+
text=True,
1054+
timeout=60,
1055+
)
1056+
if baseline.returncode:
1057+
# sys.stdlib_module_names includes modules for other
1058+
# platforms and optional extension modules not built here.
1059+
continue
1060+
importable.append(module)
1061+
1062+
for mode in ("normal", "none"):
1063+
with self.subTest(module=module, mode=mode):
1064+
result = subprocess.run(
1065+
[
1066+
sys.executable,
1067+
"-I",
1068+
"-X",
1069+
f"lazy_imports={mode}",
1070+
"-c",
1071+
code,
1072+
],
1073+
capture_output=True,
1074+
text=True,
1075+
timeout=60,
1076+
)
1077+
self.assertEqual(result.returncode, 0, result.stderr)
1078+
1079+
self.assertGreater(len(importable), 100)
1080+
10371081
def test_cli_lazy_imports_normal_respects_lazy_keyword_only(self):
10381082
"""-X lazy_imports=normal should respect lazy keyword only."""
10391083
# Note: Use test modules instead of stdlib modules to avoid
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix import cycles exposed by running standard library modules with
2+
``-X lazy_imports=none``.

0 commit comments

Comments
 (0)