diff options
author | Ran Benita <ran@unusedvar.com> | 2020-10-31 18:59:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-31 18:59:50 +0200 |
commit | 7fb0ea3f68393552441362fa1a398f7bb18f3182 (patch) | |
tree | 35c06935eee5322923dfceeeff74335f8bf3b282 /src/_pytest/pathlib.py | |
parent | 1c18fb8ccc98869c71c80bb17c7e4da34f3c2a07 (diff) | |
parent | 8a38e7a6e8039de93c6f24935effd89f034d9c00 (diff) | |
download | pytest-7fb0ea3f68393552441362fa1a398f7bb18f3182.tar.gz |
Merge pull request #7956 from csernazs/fix-7951
Fix handling recursive symlinks
Diffstat (limited to 'src/_pytest/pathlib.py')
-rw-r--r-- | src/_pytest/pathlib.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index b96cba069..6a36ae17a 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -9,6 +9,10 @@ import sys import uuid import warnings from enum import Enum +from errno import EBADF +from errno import ELOOP +from errno import ENOENT +from errno import ENOTDIR from functools import partial from os.path import expanduser from os.path import expandvars @@ -37,6 +41,24 @@ LOCK_TIMEOUT = 60 * 60 * 24 * 3 _AnyPurePath = TypeVar("_AnyPurePath", bound=PurePath) +# The following function, variables and comments were +# copied from cpython 3.9 Lib/pathlib.py file. + +# EBADF - guard against macOS `stat` throwing EBADF +_IGNORED_ERRORS = (ENOENT, ENOTDIR, EBADF, ELOOP) + +_IGNORED_WINERRORS = ( + 21, # ERROR_NOT_READY - drive exists but is not accessible + 1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself +) + + +def _ignore_error(exception): + return ( + getattr(exception, "errno", None) in _IGNORED_ERRORS + or getattr(exception, "winerror", None) in _IGNORED_WINERRORS + ) + def get_lock_path(path: _AnyPurePath) -> _AnyPurePath: return path.joinpath(".lock") @@ -555,8 +577,23 @@ def visit( Entries at each directory level are sorted. """ - entries = sorted(os.scandir(path), key=lambda entry: entry.name) + + # Skip entries with symlink loops and other brokenness, so the caller doesn't + # have to deal with it. + entries = [] + for entry in os.scandir(path): + try: + entry.is_file() + except OSError as err: + if _ignore_error(err): + continue + raise + entries.append(entry) + + entries.sort(key=lambda entry: entry.name) + yield from entries + for entry in entries: if entry.is_dir() and recurse(entry): yield from visit(entry.path, recurse) |