Skip to content

Commit 282bef7

Browse files
committed
Use context manager and add error handling to os.scandir
Signed-off-by: Filipe Laíns <lains@riseup.net>
1 parent 0da477f commit 282bef7

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,19 +1490,26 @@ def path_hook_for_FileFinder(path):
14901490
return path_hook_for_FileFinder
14911491

14921492
def _find_children(self):
1493-
for entry in _os.scandir(self.path):
1494-
if entry.name == _PYCACHE:
1495-
continue
1496-
# packages
1497-
if entry.is_dir() and '.' not in entry.name:
1498-
yield entry.name
1499-
# files
1500-
if entry.is_file():
1501-
yield from [
1502-
entry.name.removesuffix(suffix)
1503-
for suffix, _ in self._loaders
1504-
if entry.name.endswith(suffix)
1505-
]
1493+
with _os.scandir(self.path) as scan_iterator:
1494+
while True:
1495+
try:
1496+
entry = next(scan_iterator)
1497+
if entry.name == _PYCACHE:
1498+
continue
1499+
# packages
1500+
if entry.is_dir() and '.' not in entry.name:
1501+
yield entry.name
1502+
# files
1503+
if entry.is_file():
1504+
yield from [
1505+
entry.name.removesuffix(suffix)
1506+
for suffix, _ in self._loaders
1507+
if entry.name.endswith(suffix)
1508+
]
1509+
except OSError:
1510+
pass # ignore exceptions from next(scan_iterator) and os.DirEntry
1511+
except StopIteration:
1512+
break
15061513

15071514
def discover(self, parent=None):
15081515
module_prefix = f'{parent.name}.' if parent else ''

0 commit comments

Comments
 (0)