Skip to content

Commit 8fb96b0

Browse files
committed
Fix dot handling in dist-info directory name
When searching for a matching dist-info directory, try to escape dots in the name segment. This makes functions like distribution() able to accept PEP 503 normalized names to locate a distribution's dist-info directory if the distribution's name contains dots.
1 parent 5b73f25 commit 8fb96b0

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

importlib_metadata/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def search(self, name):
473473
for child in self.children():
474474
n_low = child.lower()
475475
if (n_low in name.exact_matches
476-
or n_low.startswith(name.prefix)
476+
or n_low.replace('.', '_').startswith(name.prefix)
477477
and n_low.endswith(name.suffixes)
478478
# legacy case:
479479
or self.is_egg(name) and n_low == 'egg-info'):

tests/fixtures.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ def setUp(self):
104104
build_files(DistInfoPkg.files, self.site_dir)
105105

106106

107+
class DistInfoPkgWithDot(OnSysPath, SiteDir):
108+
files = {
109+
"pkg.dot-1.0.0.dist-info": {
110+
"METADATA": """
111+
Name: pkg.dot
112+
Version: 1.0.0
113+
""",
114+
},
115+
}
116+
117+
def setUp(self):
118+
super(DistInfoPkgWithDot, self).setUp()
119+
build_files(DistInfoPkgWithDot.files, self.site_dir)
120+
121+
107122
class DistInfoPkgOffPath(SiteDir):
108123
def setUp(self):
109124
super(DistInfoPkgOffPath, self).setUp()

tests/test_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class APITests(
2323
fixtures.EggInfoPkg,
2424
fixtures.DistInfoPkg,
25+
fixtures.DistInfoPkgWithDot,
2526
fixtures.EggInfoFile,
2627
unittest.TestCase):
2728

@@ -41,6 +42,9 @@ def test_for_name_does_not_exist(self):
4142
with self.assertRaises(PackageNotFoundError):
4243
distribution('does-not-exist')
4344

45+
def test_for_name_containing_dot(self):
46+
assert distribution('pkg-dot').metadata['Name'] == 'pkg.dot'
47+
4448
def test_for_top_level(self):
4549
self.assertEqual(
4650
distribution('egginfo-pkg').read_text('top_level.txt').strip(),

0 commit comments

Comments
 (0)