Skip to content

Commit c9c0909

Browse files
committed
Switch from the very slow os.path.splitext to str.rpartition.
As a consequence, `ext` doesn't include the leading dot anymore, so change `suffixes` accordingly too.
1 parent c1af1d6 commit c9c0909

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

importlib_metadata/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,17 @@ class Prepared:
493493
"""
494494

495495
normalized = None
496-
suffixes = '.dist-info', '.egg-info'
496+
suffixes = 'dist-info', 'egg-info'
497497
exact_matches = [''][:0]
498498

499499
def __init__(self, name):
500500
self.name = name
501501
if name is None:
502502
return
503503
self.normalized = self.normalize(name)
504-
self.exact_matches = [self.normalized + suffix for suffix in self.suffixes]
504+
self.exact_matches = [
505+
self.normalized + '.' + suffix for suffix in self.suffixes
506+
]
505507

506508
@staticmethod
507509
def normalize(name):
@@ -520,8 +522,10 @@ def legacy_normalize(name):
520522

521523
def matches(self, cand, base):
522524
low = cand.lower()
523-
pre, ext = os.path.splitext(low)
524-
name, sep, rest = pre.partition('-')
525+
# rpartition is like os.path.splitext, but much faster. They'd only
526+
# differ if pre is empty, but in that case we don't have a match anyways.
527+
pre, _, ext = low.rpartition('.')
528+
name, _, rest = pre.partition('-')
525529
return (
526530
low in self.exact_matches
527531
or ext in self.suffixes

0 commit comments

Comments
 (0)