From 5d01d0cd9e39212e082640e53d3ef55a64a49b27 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 25 Sep 2023 13:06:07 +0100 Subject: [PATCH 1/3] adapters --- Lib/importlib/metadata/__init__.py | 4 +++- Lib/importlib/resources/_common.py | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py index 5c09666b6a40d9..2ad99acde1c91d 100644 --- a/Lib/importlib/metadata/__init__.py +++ b/Lib/importlib/metadata/__init__.py @@ -15,7 +15,7 @@ import collections import inspect -from . import _adapters, _meta +from . import _meta from ._collections import FreezableDefaultDict, Pair from ._functools import method_cache, pass_none from ._itertools import always_iterable, unique_everseen @@ -439,6 +439,8 @@ def metadata(self) -> _meta.PackageMetadata: The returned object will have keys that name the various bits of metadata. See PEP 566 for details. """ + from . import _adapters + opt_text = ( self.read_text('METADATA') or self.read_text('PKG-INFO') diff --git a/Lib/importlib/resources/_common.py b/Lib/importlib/resources/_common.py index a3902535342612..ccb6c1775fc47f 100644 --- a/Lib/importlib/resources/_common.py +++ b/Lib/importlib/resources/_common.py @@ -12,8 +12,6 @@ from typing import Union, Optional, cast from .abc import ResourceReader, Traversable -from ._adapters import wrap_spec - Package = Union[types.ModuleType, str] Anchor = Package @@ -109,6 +107,7 @@ def from_package(package: types.ModuleType): Return a Traversable object for the given package. """ + from ._adapters import wrap_spec spec = wrap_spec(package) reader = spec.loader.get_resource_reader(spec.name) return reader.files() From 0dc96f1837cd163d6e8f7312ef1aefdcbaa54038 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 20 Mar 2024 23:05:56 -0400 Subject: [PATCH 2/3] Add comments for deferred imports with links to rationale. --- Lib/importlib/metadata/__init__.py | 1 + Lib/importlib/resources/_common.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py index 2ad99acde1c91d..ccfbd710059e35 100644 --- a/Lib/importlib/metadata/__init__.py +++ b/Lib/importlib/metadata/__init__.py @@ -439,6 +439,7 @@ def metadata(self) -> _meta.PackageMetadata: The returned object will have keys that name the various bits of metadata. See PEP 566 for details. """ + # deferred for performance (python/cpython#109829) from . import _adapters opt_text = ( diff --git a/Lib/importlib/resources/_common.py b/Lib/importlib/resources/_common.py index ccb6c1775fc47f..e18082fb3d26a0 100644 --- a/Lib/importlib/resources/_common.py +++ b/Lib/importlib/resources/_common.py @@ -107,7 +107,9 @@ def from_package(package: types.ModuleType): Return a Traversable object for the given package. """ + # deferred for performance (python/cpython#109829) from ._adapters import wrap_spec + spec = wrap_spec(package) reader = spec.loader.get_resource_reader(spec.name) return reader.files() From 9abc8e937f53bd3029e078949c048553475965e9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 20 Mar 2024 23:08:04 -0400 Subject: [PATCH 3/3] Add blurb --- .../next/Library/2024-03-20-23-07-58.gh-issue-109653.uu3lrX.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-03-20-23-07-58.gh-issue-109653.uu3lrX.rst diff --git a/Misc/NEWS.d/next/Library/2024-03-20-23-07-58.gh-issue-109653.uu3lrX.rst b/Misc/NEWS.d/next/Library/2024-03-20-23-07-58.gh-issue-109653.uu3lrX.rst new file mode 100644 index 00000000000000..38d7634b54c2fe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-03-20-23-07-58.gh-issue-109653.uu3lrX.rst @@ -0,0 +1,2 @@ +Deferred select imports in importlib.metadata and importlib.resources for a +14% speedup.