Skip to content

Commit 9a5ca31

Browse files
authoredNov 10, 2022
[3.10] gh-99130: Apply bugfixes from importlib_metadata 4.11.4. (#99132)
1 parent e6f066a commit 9a5ca31

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed
 

‎Lib/importlib/metadata/__init__.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from . import _adapters, _meta
1818
from ._meta import PackageMetadata
1919
from ._collections import FreezableDefaultDict, Pair
20-
from ._functools import method_cache
20+
from ._functools import method_cache, pass_none
2121
from ._itertools import unique_everseen
2222
from ._meta import PackageMetadata, SimplePath
2323

@@ -938,13 +938,25 @@ def _normalized_name(self):
938938
normalized name from the file system path.
939939
"""
940940
stem = os.path.basename(str(self._path))
941-
return self._name_from_stem(stem) or super()._normalized_name
941+
return (
942+
pass_none(Prepared.normalize)(self._name_from_stem(stem))
943+
or super()._normalized_name
944+
)
942945

943-
def _name_from_stem(self, stem):
944-
name, ext = os.path.splitext(stem)
946+
@staticmethod
947+
def _name_from_stem(stem):
948+
"""
949+
>>> PathDistribution._name_from_stem('foo-3.0.egg-info')
950+
'foo'
951+
>>> PathDistribution._name_from_stem('CherryPy-3.0.dist-info')
952+
'CherryPy'
953+
>>> PathDistribution._name_from_stem('face.egg-info')
954+
'face'
955+
"""
956+
filename, ext = os.path.splitext(stem)
945957
if ext not in ('.dist-info', '.egg-info'):
946958
return
947-
name, sep, rest = stem.partition('-')
959+
name, sep, rest = filename.partition('-')
948960
return name
949961

950962

‎Lib/importlib/metadata/_functools.py

+19
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ def wrapper(self, *args, **kwargs):
8383
wrapper.cache_clear = lambda: None
8484

8585
return wrapper
86+
87+
88+
# From jaraco.functools 3.3
89+
def pass_none(func):
90+
"""
91+
Wrap func so it's not called if its first param is None
92+
93+
>>> print_text = pass_none(print)
94+
>>> print_text('text')
95+
text
96+
>>> print_text(None)
97+
"""
98+
99+
@functools.wraps(func)
100+
def wrapper(param, *args, **kwargs):
101+
if param is not None:
102+
return func(param, *args, **kwargs)
103+
104+
return wrapper

‎Lib/test/test_importlib/test_metadata_api.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ def test_entry_points_distribution(self):
8989
self.assertIn(ep.dist.name, ('distinfo-pkg', 'egginfo-pkg'))
9090
self.assertEqual(ep.dist.version, "1.0.0")
9191

92-
def test_entry_points_unique_packages(self):
93-
# Entry points should only be exposed for the first package
94-
# on sys.path with a given name.
92+
def test_entry_points_unique_packages_normalized(self):
93+
"""
94+
Entry points should only be exposed for the first package
95+
on sys.path with a given name (even when normalized).
96+
"""
9597
alt_site_dir = self.fixtures.enter_context(fixtures.tempdir())
9698
self.fixtures.enter_context(self.add_sys_path(alt_site_dir))
9799
alt_pkg = {
98-
"distinfo_pkg-1.1.0.dist-info": {
100+
"DistInfo_pkg-1.1.0.dist-info": {
99101
"METADATA": """
100102
Name: distinfo-pkg
101103
Version: 1.1.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Apply bugfixes from `importlib_metadata 4.11.4
2+
<https://importlib-metadata.readthedocs.io/en/latest/history.html#v4-11-4>`_,
3+
namely: In ``PathDistribution._name_from_stem``, avoid including parts of
4+
the extension in the result. In ``PathDistribution._normalized_name``,
5+
ensure names loaded from the stem of the filename are also normalized,
6+
ensuring duplicate entry points by packages varying only by non-normalized
7+
name are hidden.

0 commit comments

Comments
 (0)
Please sign in to comment.