diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2022-06-26 01:04:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-26 01:04:28 (GMT) |
commit | 38612a05b5de33fde82d7960418527b7cfaa2e7c (patch) | |
tree | fc311ccb634a3bf6fb025bf5b064ead486e7be25 /Lib/importlib/metadata | |
parent | 9af6b75298d066e89646acf8df1704bef183a6f8 (diff) | |
download | cpython-38612a05b5de33fde82d7960418527b7cfaa2e7c.zip cpython-38612a05b5de33fde82d7960418527b7cfaa2e7c.tar.gz cpython-38612a05b5de33fde82d7960418527b7cfaa2e7c.tar.bz2 |
gh-93259: Validate arg to ``Distribution.from_name``. (GH-94270)
Syncs with importlib_metadata 4.12.0.
Diffstat (limited to 'Lib/importlib/metadata')
-rw-r--r-- | Lib/importlib/metadata/__init__.py | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py index 9ceae8a..b01de14 100644 --- a/Lib/importlib/metadata/__init__.py +++ b/Lib/importlib/metadata/__init__.py @@ -543,7 +543,7 @@ class Distribution: """ @classmethod - def from_name(cls, name): + def from_name(cls, name: str): """Return the Distribution for the given package name. :param name: The name of the distribution package to search for. @@ -551,13 +551,13 @@ class Distribution: package, if found. :raises PackageNotFoundError: When the named package's distribution metadata cannot be found. + :raises ValueError: When an invalid value is supplied for name. """ - for resolver in cls._discover_resolvers(): - dists = resolver(DistributionFinder.Context(name=name)) - dist = next(iter(dists), None) - if dist is not None: - return dist - else: + if not name: + raise ValueError("A distribution name is required.") + try: + return next(cls.discover(name=name)) + except StopIteration: raise PackageNotFoundError(name) @classmethod @@ -945,13 +945,26 @@ class PathDistribution(Distribution): normalized name from the file system path. """ stem = os.path.basename(str(self._path)) - return self._name_from_stem(stem) or super()._normalized_name + return ( + pass_none(Prepared.normalize)(self._name_from_stem(stem)) + or super()._normalized_name + ) - def _name_from_stem(self, stem): - name, ext = os.path.splitext(stem) + @staticmethod + def _name_from_stem(stem): + """ + >>> PathDistribution._name_from_stem('foo-3.0.egg-info') + 'foo' + >>> PathDistribution._name_from_stem('CherryPy-3.0.dist-info') + 'CherryPy' + >>> PathDistribution._name_from_stem('face.egg-info') + 'face' + >>> PathDistribution._name_from_stem('foo.bar') + """ + filename, ext = os.path.splitext(stem) if ext not in ('.dist-info', '.egg-info'): return - name, sep, rest = stem.partition('-') + name, sep, rest = filename.partition('-') return name @@ -991,6 +1004,15 @@ def version(distribution_name): return distribution(distribution_name).version +_unique = functools.partial( + unique_everseen, + key=operator.attrgetter('_normalized_name'), +) +""" +Wrapper for ``distributions`` to return unique distributions by name. +""" + + def entry_points(**params) -> Union[EntryPoints, SelectableGroups]: """Return EntryPoint objects for all installed packages. @@ -1008,10 +1030,8 @@ def entry_points(**params) -> Union[EntryPoints, SelectableGroups]: :return: EntryPoints or SelectableGroups for all installed packages. """ - norm_name = operator.attrgetter('_normalized_name') - unique = functools.partial(unique_everseen, key=norm_name) eps = itertools.chain.from_iterable( - dist.entry_points for dist in unique(distributions()) + dist.entry_points for dist in _unique(distributions()) ) return SelectableGroups.load(eps).select(**params) |