From 12fbb96c41ecadb164e314ab3672461f8946cd19 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 24 Jul 2026 15:59:11 -0700 Subject: [PATCH 1/3] Use lazy imports in the top-level modules of importlib --- Lib/importlib/machinery.py | 6 ++++-- Lib/importlib/util.py | 17 +++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py index 023f77d750fd2bc..b22e538862299ff 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -1,5 +1,9 @@ """The machinery of importlib: finders, loaders, hooks, etc.""" +lazy import warnings + + +# Public API from ._bootstrap import ModuleSpec from ._bootstrap import BuiltinImporter from ._bootstrap import FrozenImporter @@ -33,8 +37,6 @@ def all_suffixes(): def __getattr__(name): - import warnings - if name == 'DEBUG_BYTECODE_SUFFIXES': warnings.warn('importlib.machinery.DEBUG_BYTECODE_SUFFIXES is ' 'deprecated; use importlib.machinery.BYTECODE_SUFFIXES ' diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index 2b564e9b52e0cb2..52861e378c65e0e 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -1,19 +1,24 @@ """Utility code for constructing importers, etc.""" + +from ._bootstrap import _resolve_name +from ._bootstrap import _find_spec + +import sys +import types + +lazy import _imp + + +# Public API from ._abc import Loader from ._bootstrap import module_from_spec -from ._bootstrap import _resolve_name from ._bootstrap import spec_from_loader -from ._bootstrap import _find_spec from ._bootstrap_external import MAGIC_NUMBER from ._bootstrap_external import cache_from_source from ._bootstrap_external import decode_source from ._bootstrap_external import source_from_cache from ._bootstrap_external import spec_from_file_location -import _imp -import sys -import types - def source_hash(source_bytes): "Return the hash of *source_bytes* as used in hash-based pyc files." From 457b553dd370ac236a722cbe27c5222221f59d6a Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 27 Jul 2026 12:41:32 -0700 Subject: [PATCH 2/3] Revert making `_imp` lazy as it's a built-in module --- Lib/importlib/util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index 52861e378c65e0e..dbce696dc8d169a 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -3,11 +3,10 @@ from ._bootstrap import _resolve_name from ._bootstrap import _find_spec +import _imp import sys import types -lazy import _imp - # Public API from ._abc import Loader From a71ce978ee1292a8a1b1dcd4764fb2a7977660b0 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 27 Jul 2026 12:48:16 -0700 Subject: [PATCH 3/3] Reorder imports in `importlib.abc` for clarity and consistency --- Lib/importlib/abc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py index 9ca127ad9c7d0fd..ec68379e76f6f31 100644 --- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -1,6 +1,7 @@ """Abstract base classes related to import.""" from . import _bootstrap_external from . import machinery + try: import _frozen_importlib except ImportError as exc: @@ -11,10 +12,12 @@ import _frozen_importlib_external except ImportError: _frozen_importlib_external = _bootstrap_external -from ._abc import Loader import abc +# Public API +from ._abc import Loader + __all__ = [ 'Loader', 'MetaPathFinder', 'PathEntryFinder', 'ResourceLoader', 'InspectLoader', 'ExecutionLoader',