Skip to content

Commit 6cd9b2f

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

4 files changed

Lines changed: 64 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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,63 @@ 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+
env = {**os.environ, "BROWSER": "true"}
1040+
importable = []
1041+
1042+
for module in sorted(sys.stdlib_module_names):
1043+
with self.subTest(module=module):
1044+
code = f"import importlib; importlib.import_module({module!r})"
1045+
baseline = subprocess.run(
1046+
[sys.executable, "-I", "-c", code],
1047+
capture_output=True,
1048+
env=env,
1049+
text=True,
1050+
timeout=60,
1051+
)
1052+
if baseline.returncode:
1053+
# sys.stdlib_module_names includes modules for other
1054+
# platforms and optional extension modules not built here.
1055+
continue
1056+
importable.append(module)
1057+
1058+
result = subprocess.run(
1059+
[
1060+
sys.executable,
1061+
"-I",
1062+
"-X",
1063+
"lazy_imports=none",
1064+
"-c",
1065+
code,
1066+
],
1067+
capture_output=True,
1068+
env=env,
1069+
text=True,
1070+
timeout=60,
1071+
)
1072+
self.assertEqual(result.returncode, 0, result.stderr)
1073+
1074+
self.assertGreater(len(importable), 100)
1075+
1076+
def test_cli_lazy_imports_none_starts_test_runner(self):
1077+
"""-X lazy_imports=none should not break ``python -m test``."""
1078+
result = subprocess.run(
1079+
[
1080+
sys.executable,
1081+
"-X",
1082+
"lazy_imports=none",
1083+
"-m",
1084+
"test",
1085+
"--list-tests",
1086+
"test_lazy_import",
1087+
],
1088+
capture_output=True,
1089+
text=True,
1090+
)
1091+
self.assertEqual(result.returncode, 0, result.stderr)
1092+
self.assertIn("test_lazy_import", result.stdout)
1093+
10371094
def test_cli_lazy_imports_normal_respects_lazy_keyword_only(self):
10381095
"""-X lazy_imports=normal should respect lazy keyword only."""
10391096
# 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)