Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,13 @@ def _find_children(self):
while True:
try:
entry = next(scan_iterator)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply entry = next(scan_iterator, None) followed by if entry is None? we're already assuming that the entry is non-None since we do entry.name just after next.

except StopIteration:
break
except OSError:
# A failing scan cannot make progress; end the listing
# like _fill_cache() treats an unreadable directory.
break
try:
if entry.name == _PYCACHE:
continue
# packages
Expand All @@ -1467,9 +1474,7 @@ def _find_children(self):
if entry.name.endswith(suffix)
}
except OSError:
pass # ignore exceptions from next(scan_iterator) and os.DirEntry
except StopIteration:
break
pass # skip entries whose os.DirEntry methods fail

def discover(self, parent=None):
if parent and parent.submodule_search_locations is None:
Expand Down
64 changes: 63 additions & 1 deletion Lib/test/test_importlib/test_discover.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import Mock
from unittest.mock import Mock, patch

from test.test_importlib import util

Expand Down Expand Up @@ -114,6 +114,68 @@ def test_invalid_parent(self):
with self.assertRaises(ValueError):
list(finder.discover(example))

def _patch_scandir(self, scandir):
module_os = self.machinery.FileFinder._fill_cache.__globals__['_os']
return patch.object(module_os, 'scandir', scandir)

def test_discover_persistently_failing_scan(self):
# gh-155935: an iterator that raises OSError on every next() call
# must end the listing instead of looping forever.
class FailingScandirIterator:
calls = 0

def __enter__(self):
return self

def __exit__(self, *args):
return False

def __next__(self):
self.calls += 1
if self.calls > 100:
# Safety net so regressed code fails fast on the call
# count below instead of hanging the test forever.
raise StopIteration
raise OSError('persistently failing directory scan')

scan_iterator = FailingScandirIterator()
with self._patch_scandir(lambda path: scan_iterator):
finder = self.get_finder('dummy')
discovered = list(finder.discover())
self.assertEqual(discovered, [])
# A failed scan must not be retried.
self.assertEqual(scan_iterator.calls, 1)

def test_find_children_failing_direntry(self):
# An entry whose DirEntry methods raise OSError is skipped; the
# remaining entries are still listed.
failing = Mock()
failing.name = 'failing'
failing.is_dir.side_effect = OSError('stat failed')
good = Mock()
good.name = 'example.py'
good.is_dir.return_value = False
good.is_file.return_value = True

class FakeScandirIterator:
def __init__(self, entries):
self._iterator = iter(entries)

def __enter__(self):
return self

def __exit__(self, *args):
return False

def __next__(self):
return next(self._iterator)

with self._patch_scandir(
lambda path: FakeScandirIterator([failing, good])):
finder = self.get_finder('dummy')
children = list(finder._find_children())
self.assertEqual(children, ['example'])


(
Frozen_TestFileFinder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :meth:`!importlib.machinery.FileFinder.discover` looping forever when the underlying directory scan keeps failing with :exc:`OSError`. A failing scan now ends the listing instead of being retried.
Loading