mirror of
https://github.com/python/cpython.git
synced 2026-05-06 04:37:33 -04:00
gh-149321: Fix stdlib imports with lazy imports disabled (#149338)
This commit is contained in:
committed by
GitHub
parent
bc285e5832
commit
12a20daee7
+3
-1
@@ -21,7 +21,6 @@ that work tightly with the python syntax (template engines for example).
|
||||
:license: Python License.
|
||||
"""
|
||||
from _ast import *
|
||||
lazy from _colorize import can_colorize, get_theme
|
||||
|
||||
|
||||
def parse(source, filename='<unknown>', mode='exec', *,
|
||||
@@ -142,6 +141,8 @@ def dump(
|
||||
If show_empty is False, then empty lists and fields that are None
|
||||
will be omitted from the output for better readability.
|
||||
"""
|
||||
from _colorize import get_theme
|
||||
|
||||
t = get_theme(force_color=color, force_no_color=not color).ast
|
||||
|
||||
def _format(node, level=0):
|
||||
@@ -708,6 +709,7 @@ def main(args=None):
|
||||
|
||||
tree = parse(source, name, args.mode, type_comments=args.no_type_comments,
|
||||
feature_version=feature_version, optimize=args.optimize)
|
||||
from _colorize import can_colorize
|
||||
print(dump(tree, include_attributes=args.include_attributes,
|
||||
color=can_colorize(file=sys.stdout),
|
||||
indent=args.indent, show_empty=args.show_empty))
|
||||
|
||||
+2
-1
@@ -6,7 +6,6 @@ import annotationlib
|
||||
import abc
|
||||
from reprlib import recursive_repr
|
||||
lazy import copy
|
||||
lazy import inspect
|
||||
lazy import re
|
||||
|
||||
|
||||
@@ -981,6 +980,7 @@ class _AutoDocstring:
|
||||
try:
|
||||
# In some cases fetching a signature is not possible.
|
||||
# But, we surely should not fail in this case.
|
||||
import inspect
|
||||
text_sig = str(inspect.signature(
|
||||
cls,
|
||||
annotation_format=annotationlib.Format.FORWARDREF,
|
||||
@@ -1391,6 +1391,7 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
|
||||
|
||||
# If this is a wrapped function, unwrap it.
|
||||
if not isinstance(member, type) and hasattr(member, '__wrapped__'):
|
||||
import inspect
|
||||
member = inspect.unwrap(member)
|
||||
|
||||
if isinstance(member, types.FunctionType):
|
||||
|
||||
@@ -1034,6 +1034,50 @@ class CommandLineAndEnvVarTests(unittest.TestCase):
|
||||
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
|
||||
self.assertIn("EAGER", result.stdout)
|
||||
|
||||
@support.requires_resource("cpu")
|
||||
def test_cli_lazy_imports_modes_import_stdlib_modules(self):
|
||||
"""-X lazy_imports modes should import available stdlib modules."""
|
||||
# Do not smoke-test modules with intentional import-time effects.
|
||||
import_side_effect_modules = {"antigravity", "this"}
|
||||
importable = []
|
||||
|
||||
for module in sorted(sys.stdlib_module_names):
|
||||
if module in import_side_effect_modules:
|
||||
continue
|
||||
|
||||
with self.subTest(module=module):
|
||||
code = f"import {module}; print({module})"
|
||||
baseline = subprocess.run(
|
||||
[sys.executable, "-I", "-c", code],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60,
|
||||
)
|
||||
if baseline.returncode:
|
||||
# sys.stdlib_module_names includes modules for other
|
||||
# platforms and optional extension modules not built here.
|
||||
continue
|
||||
importable.append(module)
|
||||
|
||||
for mode in ("normal", "none"):
|
||||
with self.subTest(module=module, mode=mode):
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
"-I",
|
||||
"-X",
|
||||
f"lazy_imports={mode}",
|
||||
"-c",
|
||||
code,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60,
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
self.assertGreater(len(importable), 100)
|
||||
|
||||
def test_cli_lazy_imports_normal_respects_lazy_keyword_only(self):
|
||||
"""-X lazy_imports=normal should respect lazy keyword only."""
|
||||
# Note: Use test modules instead of stdlib modules to avoid
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
Fix import cycles exposed by running standard library modules with
|
||||
``-X lazy_imports=none``.
|
||||
Reference in New Issue
Block a user