Skip to content

Commit 050c3ff

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

4 files changed

Lines changed: 53 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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,52 @@ 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+
def test_cli_lazy_imports_none_imports_stdlib_modules(self):
1038+
"""-X lazy_imports=none should import all available stdlib modules."""
1039+
import importlib.util
1040+
1041+
# Do not smoke-test modules with intentional import-time effects.
1042+
import_side_effect_modules = {"antigravity", "this"}
1043+
code = "import importlib, sys; importlib.import_module(sys.argv[1])"
1044+
importable = []
1045+
1046+
for module in sorted(sys.stdlib_module_names):
1047+
if module in import_side_effect_modules:
1048+
continue
1049+
if importlib.util.find_spec(module) is None:
1050+
continue
1051+
1052+
with self.subTest(module=module):
1053+
baseline = subprocess.run(
1054+
[sys.executable, "-I", "-c", code, module],
1055+
capture_output=True,
1056+
text=True,
1057+
timeout=60,
1058+
)
1059+
if baseline.returncode:
1060+
# sys.stdlib_module_names includes modules for other
1061+
# platforms and optional extension modules not built here.
1062+
continue
1063+
importable.append(module)
1064+
1065+
result = subprocess.run(
1066+
[
1067+
sys.executable,
1068+
"-I",
1069+
"-X",
1070+
"lazy_imports=none",
1071+
"-c",
1072+
code,
1073+
module,
1074+
],
1075+
capture_output=True,
1076+
text=True,
1077+
timeout=60,
1078+
)
1079+
self.assertEqual(result.returncode, 0, result.stderr)
1080+
1081+
self.assertGreater(len(importable), 100)
1082+
10371083
def test_cli_lazy_imports_normal_respects_lazy_keyword_only(self):
10381084
"""-X lazy_imports=normal should respect lazy keyword only."""
10391085
# 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)