Skip to content

Commit 11c147f

Browse files
committed
Extract 'list dir' and 'list zip' as separate functions. Store 'path_type' in instance attribute.
1 parent 43d9d18 commit 11c147f

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

importlib_metadata/__init__.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,24 +392,26 @@ class FastPath:
392392
children.
393393
"""
394394

395+
path_type = pathlib.Path
396+
395397
def __init__(self, root):
396398
self.root = root
397399

398400
def children(self):
399-
try:
400-
children = os.listdir(self.root or '.')
401-
path_type = pathlib.Path
402-
except Exception:
403-
try:
404-
with zipfile.ZipFile(self.root) as zf:
405-
children = [os.path.split(child)[0]
406-
for child in zf.namelist()]
407-
path_type = zipp.Path
408-
except Exception:
409-
children = []
410-
path_type = None
401+
with suppress(Exception):
402+
yield from os.listdir(self.root or '')
403+
with suppress(Exception):
404+
yield from self.zip_children()
405+
406+
def zip_children(self):
407+
with zipfile.ZipFile(self.root) as zf:
408+
names = zf.namelist()
409+
self.path_type = zipp.Path
411410

412-
return children, path_type
411+
return (
412+
os.path.split(child)[0]
413+
for child in names
414+
)
413415

414416

415417
@install
@@ -453,14 +455,13 @@ def _search_path(cls, root, name):
453455
root_is_egg = (
454456
root_n_low == normalized + '.egg'
455457
or root_n_low.startswith(prefix) and root_n_low.endswith('.egg'))
456-
children, path_type = root.children()
457-
for child in children:
458+
for child in root.children():
458459
n_low = child.lower()
459460
if (n_low in exact_matches
460461
or n_low.startswith(prefix) and n_low.endswith(suffixes)
461462
# legacy case:
462463
or root_is_egg and n_low == 'egg-info'):
463-
yield path_type(root.root, child)
464+
yield root.path_type(root.root, child)
464465

465466

466467
class PathDistribution(Distribution):

0 commit comments

Comments
 (0)