Skip to content

Commit ad58bb0

Browse files
benoit-pierrejaraco
authored andcommitted
fix PathDistribution._normalized_name implementation
- apply PEP 503 normalization to the extracted names (e.g.: `zope..inter_face-4.2.dist-info` must yield the name `zope_inter_face`) `entry_points(…)` can yield the entry-points of a shadowed distribution. For example: with a version of `mypkg` in the system' site-packages directory when working from another development checkout of the same package (with a `mypkg.egg-info` directory mishandled by the first bug).
1 parent a92fbf3 commit ad58bb0

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

importlib_metadata/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@ def _normalized_name(self):
956956
normalized name from the file system path.
957957
"""
958958
stem = os.path.basename(str(self._path))
959-
return self._name_from_stem(stem) or super()._normalized_name
959+
from_fs = pass_none(Prepared.normalize)(self._name_from_stem(stem))
960+
return from_fs or super()._normalized_name
960961

961962
@staticmethod
962963
def _name_from_stem(stem):
@@ -967,6 +968,7 @@ def _name_from_stem(stem):
967968
'CherryPy'
968969
>>> PathDistribution._name_from_stem('face.egg-info')
969970
'face'
971+
>>> PathDistribution._name_from_stem('foo.bar')
970972
"""
971973
filename, ext = os.path.splitext(stem)
972974
if ext not in ('.dist-info', '.egg-info'):

tests/test_main.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
EntryPoint,
1515
MetadataPathFinder,
1616
PackageNotFoundError,
17+
distribution,
1718
distributions,
1819
entry_points,
1920
metadata,
@@ -87,15 +88,17 @@ def pkg_with_dashes(site_dir):
8788
metadata = metadata_dir / 'METADATA'
8889
with metadata.open('w', encoding='utf-8') as strm:
8990
strm.write('Version: 1.0\n')
90-
return 'my-pkg'
91+
return 'my-pkg', 'my_pkg'
9192

9293
def test_dashes_in_dist_name_found_as_underscores(self):
9394
"""
9495
For a package with a dash in the name, the dist-info metadata
9596
uses underscores in the name. Ensure the metadata loads.
9697
"""
97-
pkg_name = self.pkg_with_dashes(self.site_dir)
98-
assert version(pkg_name) == '1.0'
98+
pkg_name, norm_pkg_name = self.pkg_with_dashes(self.site_dir)
99+
dist = distribution(pkg_name)
100+
assert dist._normalized_name == norm_pkg_name
101+
assert dist.version == '1.0'
99102

100103
@staticmethod
101104
def pkg_with_mixed_case(site_dir):
@@ -108,16 +111,40 @@ def pkg_with_mixed_case(site_dir):
108111
metadata = metadata_dir / 'METADATA'
109112
with metadata.open('w', encoding='utf-8') as strm:
110113
strm.write('Version: 1.0\n')
111-
return 'CherryPy'
114+
return 'CheRRypY', 'cherrypy'
112115

113116
def test_dist_name_found_as_any_case(self):
114117
"""
115118
Ensure the metadata loads when queried with any case.
116119
"""
117-
pkg_name = self.pkg_with_mixed_case(self.site_dir)
118-
assert version(pkg_name) == '1.0'
119-
assert version(pkg_name.lower()) == '1.0'
120-
assert version(pkg_name.upper()) == '1.0'
120+
pkg_name, norm_pkg_name = self.pkg_with_mixed_case(self.site_dir)
121+
for name_variant in (pkg_name, pkg_name.lower(), pkg_name.upper()):
122+
dist = distribution(name_variant)
123+
assert dist._normalized_name == norm_pkg_name
124+
assert dist.version == '1.0'
125+
126+
@staticmethod
127+
def pkg_with_replaced_chars(site_dir):
128+
"""
129+
Create minimal metadata for a package with some
130+
characters replaced by PEP 503 normalization
131+
in the name.
132+
"""
133+
metadata_dir = site_dir / 'zope..inter_face-4.2.dist-info'
134+
metadata_dir.mkdir()
135+
metadata = metadata_dir / 'METADATA'
136+
with metadata.open('w', encoding='utf-8') as strm:
137+
strm.write('Version: 4.2\n')
138+
return 'zope-inter._FACE', 'zope_inter_face'
139+
140+
def test_dist_name_pep503_normalization(self):
141+
"""
142+
Ensure the distribution name is properly normalized.
143+
"""
144+
pkg_name, norm_pkg_name = self.pkg_with_replaced_chars(self.site_dir)
145+
dist = distribution(pkg_name)
146+
assert dist._normalized_name == norm_pkg_name
147+
assert dist.version == '4.2'
121148

122149

123150
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):

0 commit comments

Comments
 (0)