Skip to content

Commit

Permalink
Switch from the very slow os.path.splitext to str.rpartition.
Browse files Browse the repository at this point in the history
As a consequence, `ext` doesn't include the leading dot anymore, so
change `suffixes` accordingly too.
  • Loading branch information
anntzer committed Jan 10, 2021
1 parent c1af1d6 commit c9c0909
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,17 @@ class Prepared:
"""

normalized = None
suffixes = '.dist-info', '.egg-info'
suffixes = 'dist-info', 'egg-info'
exact_matches = [''][:0]

def __init__(self, name):
self.name = name
if name is None:
return
self.normalized = self.normalize(name)
self.exact_matches = [self.normalized + suffix for suffix in self.suffixes]
self.exact_matches = [
self.normalized + '.' + suffix for suffix in self.suffixes
]

@staticmethod
def normalize(name):
Expand All @@ -520,8 +522,10 @@ def legacy_normalize(name):

def matches(self, cand, base):
low = cand.lower()
pre, ext = os.path.splitext(low)
name, sep, rest = pre.partition('-')
# rpartition is like os.path.splitext, but much faster. They'd only
# differ if pre is empty, but in that case we don't have a match anyways.
pre, _, ext = low.rpartition('.')
name, _, rest = pre.partition('-')
return (
low in self.exact_matches
or ext in self.suffixes
Expand Down

0 comments on commit c9c0909

Please sign in to comment.