Skip to content
Closed
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Alexander Johnson
Alexander King
Alexei Kozlenok
Alice Purcell
Alison Day
Allan Feldman
Aly Sivji
Amir Elkess
Expand Down
1 change: 1 addition & 0 deletions changelog/13083.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes an issue where the :func:`_pytest.pathlib.scandir` function crashes if the directory to scan does not exist by catching the ``FileNotFoundError`` and returning an empty array.
26 changes: 15 additions & 11 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,17 +957,21 @@ def scandir(
The default is to sort by name.
"""
entries = []
with os.scandir(path) as s:
# Skip entries with symlink loops and other brokenness, so the caller
# doesn't have to deal with it.
for entry in s:
try:
entry.is_file()
except OSError as err:
if _ignore_error(err):
continue
raise
entries.append(entry)
try:
with os.scandir(path) as s:
scanned = list(s)
except FileNotFoundError:
return []
# Skip entries with symlink loops and other brokenness, so the caller
# doesn't have to deal with it.
for entry in scanned:
try:
entry.is_file()
except OSError as err:
if _ignore_error(err):
continue
raise
entries.append(entry)
entries.sort(key=sort_key) # type: ignore[arg-type]
return entries

Expand Down