From 0d1d8d97a0b8f2a35eadea6215fcdcdcf681c1d7 Mon Sep 17 00:00:00 2001 From: Alison Day Date: Mon, 23 Dec 2024 22:09:50 -0700 Subject: [PATCH 1/2] Catches directory not found error in scandir function --- src/_pytest/pathlib.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 25dc69b6349..0acb8291ec0 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -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 From e12a614a652972de8c3664ae7ea505439797dbb2 Mon Sep 17 00:00:00 2001 From: Alison Day Date: Mon, 23 Dec 2024 22:33:31 -0700 Subject: [PATCH 2/2] Catches directory not found error in scandir function to prevent crashing --- AUTHORS | 1 + changelog/13083.bugfix.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/13083.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 9629e00bcfb..0cd3d617fd6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -21,6 +21,7 @@ Alexander Johnson Alexander King Alexei Kozlenok Alice Purcell +Alison Day Allan Feldman Aly Sivji Amir Elkess diff --git a/changelog/13083.bugfix.rst b/changelog/13083.bugfix.rst new file mode 100644 index 00000000000..329cc70023e --- /dev/null +++ b/changelog/13083.bugfix.rst @@ -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.