Skip to content

Commit

Permalink
pythonGH-109653: Defer import of importlib.metadata._adapters (py…
Browse files Browse the repository at this point in the history
…thon#109829)

* adapters

* Add comments for deferred imports with links to rationale.

* Add blurb

---------

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
  • Loading branch information
2 people authored and adorilson committed Mar 25, 2024
1 parent 23b9b66 commit acdc574
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Lib/importlib/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import posixpath
import collections

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
Expand Down Expand Up @@ -456,6 +456,9 @@ def metadata(self) -> _meta.PackageMetadata:
Custom providers may provide the METADATA file or override this
property.
"""
# deferred for performance (python/cpython#109829)
from . import _adapters

opt_text = (
self.read_text('METADATA')
or self.read_text('PKG-INFO')
Expand Down
5 changes: 3 additions & 2 deletions Lib/importlib/resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -109,6 +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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deferred select imports in importlib.metadata and importlib.resources for a
14% speedup.

0 comments on commit acdc574

Please sign in to comment.