Skip to content

Commit

Permalink
devel/py-setuptools: backport python/importlib_metadata@6bee315
Browse files Browse the repository at this point in the history
  • Loading branch information
rozhuk-im committed Sep 17, 2024
1 parent add5f5a commit 06e03e9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--- setuptools/_vendor/importlib_metadata/__init__.py.orig 2024-09-17 03:55:02 UTC
+++ setuptools/_vendor/importlib_metadata/__init__.py
@@ -14,7 +14,7 @@ import collections
import posixpath
import collections

-from . import _adapters, _meta
+from . import _adapters, _meta, _py39compat
from ._collections import FreezableDefaultDict, Pair
from ._compat import (
NullFinder,
@@ -180,6 +180,10 @@ class EntryPoint(DeprecatedTuple):
following the attr, and following any extras.
"""
+ name: str
+ value: str
+ group: str
+
dist: Optional['Distribution'] = None
def __init__(self, name, value, group):
@@ -350,7 +354,8 @@ class EntryPoints(DeprecatedList):
Select entry points from self that match the
given parameters (typically group and/or name).
"""
- return EntryPoints(ep for ep in self if ep.matches(**params))
+ candidates = (_py39compat.ep_matches(ep, **params) for ep in self)
+ return EntryPoints(ep for ep, predicate in candidates if predicate)

@property
def names(self):
@@ -991,7 +996,7 @@ def entry_points(**params) -> Union[EntryPoints, Selec

:return: EntryPoints or SelectableGroups for all installed packages.
"""
- norm_name = operator.attrgetter('_normalized_name')
+ norm_name = _py39compat.normalized_name
unique = functools.partial(unique_everseen, key=norm_name)
eps = itertools.chain.from_iterable(
dist.entry_points for dist in unique(distributions())
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--- setuptools/_vendor/importlib_metadata/_py39compat.py.orig 2024-09-17 04:01:17 UTC
+++ setuptools/_vendor/importlib_metadata/_py39compat.py
@@ -0,0 +1,48 @@
+"""
+Compatibility layer with Python 3.8/3.9
+"""
+from typing import TYPE_CHECKING, Any, Optional, Tuple
+
+if TYPE_CHECKING: # pragma: no cover
+ # Prevent circular imports on runtime.
+ from . import Distribution, EntryPoint
+else:
+ Distribution = EntryPoint = Any
+
+
+def normalized_name(dist: Distribution) -> Optional[str]:
+ """
+ Honor name normalization for distributions that don't provide ``_normalized_name``.
+ """
+ try:
+ return dist._normalized_name
+ except AttributeError:
+ from . import Prepared # -> delay to prevent circular imports.
+
+ return Prepared.normalize(getattr(dist, "name", None) or dist.metadata['Name'])
+
+
+def ep_matches(ep: EntryPoint, **params) -> Tuple[EntryPoint, bool]:
+ """
+ Workaround for ``EntryPoint`` objects without the ``matches`` method.
+ For the sake of convenience, a tuple is returned containing not only the
+ boolean value corresponding to the predicate evalutation, but also a compatible
+ ``EntryPoint`` object that can be safely used at a later stage.
+
+ For example, the following sequences of expressions should be compatible:
+
+ # Sequence 1: using the compatibility layer
+ candidates = (_py39compat.ep_matches(ep, **params) for ep in entry_points)
+ [ep for ep, predicate in candidates if predicate]
+
+ # Sequence 2: using Python 3.9+
+ [ep for ep in entry_points if ep.matches(**params)]
+ """
+ try:
+ return ep, ep.matches(**params)
+ except AttributeError:
+ from . import EntryPoint # -> delay to prevent circular imports.
+
+ # Reconstruct the EntryPoint object to make sure it is compatible.
+ _ep = EntryPoint(ep.name, ep.value, ep.group)
+ return _ep, _ep.matches(**params)

0 comments on commit 06e03e9

Please sign in to comment.