Skip to content

Commit 9f709bd

Browse files
committed
Less paths, more speed.
Save another 25-50% in runtime (i.e. up to ~2x) by constructing fewer Path objects.
1 parent c05d316 commit 9f709bd

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

importlib_metadata/__init__.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import csv
88
import sys
99
import zipp
10+
import zipfile
1011
import operator
1112
import functools
1213
import itertools
@@ -376,8 +377,9 @@ def path(self):
376377
return vars(self).get('path', sys.path)
377378

378379
@property
379-
def pattern(self): # Now unused, could be deprecated?
380-
return '.*' if self.name is None else re.escape(self.name)
380+
def pattern(self):
381+
return ('.*' if self.name is None
382+
else re.escape(self.name)) # pragma: nocover
381383

382384
@abc.abstractmethod
383385
def find_distributions(self, context=Context()):
@@ -414,9 +416,7 @@ def find_distributions(self, context=DistributionFinder.Context()):
414416
def _search_paths(cls, name, paths):
415417
"""Find metadata directories in paths heuristically."""
416418
return itertools.chain.from_iterable(
417-
cls._search_path(path, name)
418-
for path in map(cls._switch_path, paths)
419-
)
419+
cls._search_path(path, name) for path in paths)
420420

421421
@staticmethod
422422
def _switch_path(path):
@@ -427,23 +427,26 @@ def _switch_path(path):
427427

428428
@classmethod
429429
def _search_path(cls, root, name):
430-
if not root.is_dir():
431-
return
432430
# This function is microoptimized by avoiding the use of regexes and
433431
# using strs rather than Path objects.
432+
root = root or '.'
433+
try:
434+
children = os.listdir(root)
435+
except Exception:
436+
try:
437+
with zipfile.ZipFile(root) as zf:
438+
children = [os.path.split(child)[0]
439+
for child in zf.namelist()]
440+
except Exception:
441+
return
434442
if name is not None:
435443
normalized = name.lower().replace('-', '_')
436444
prefix = normalized + '-'
437445
else:
438446
normalized = prefix = ''
439447
suffixes = ('.dist-info', '.egg-info')
440448
exact_matches = [normalized + suffix for suffix in suffixes]
441-
if isinstance(root, zipp.Path):
442-
root_n_low = os.path.split(root.root.filename.lower())[1].lower()
443-
children = [path.name for path in root.iterdir()]
444-
else: # Normal Path.
445-
root_n_low = root.name.lower()
446-
children = os.listdir(str(root))
449+
root_n_low = os.path.split(root)[1].lower()
447450
root_is_egg = (
448451
root_n_low == normalized + '.egg'
449452
or root_n_low.startswith(prefix) and root_n_low.endswith('.egg'))
@@ -453,7 +456,7 @@ def _search_path(cls, root, name):
453456
or n_low.startswith(prefix) and n_low.endswith(suffixes)
454457
# legacy case:
455458
or root_is_egg and n_low == 'egg-info'):
456-
yield root / child
459+
yield cls._switch_path(root) / child
457460

458461

459462
class PathDistribution(Distribution):

0 commit comments

Comments
 (0)