Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 506689b

Browse files
committedSep 7, 2023
Use _pytest.pathlib.safe_exists in get_dirs_from_args
Related to pytest-dev#11394
1 parent 884b911 commit 506689b

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed
 

‎src/_pytest/config/findpaths.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from _pytest.outcomes import fail
1717
from _pytest.pathlib import absolutepath
1818
from _pytest.pathlib import commonpath
19+
from _pytest.pathlib import safe_exists
1920

2021
if TYPE_CHECKING:
2122
from . import Config
@@ -151,14 +152,6 @@ def get_dir_from_path(path: Path) -> Path:
151152
return path
152153
return path.parent
153154

154-
def safe_exists(path: Path) -> bool:
155-
# This can throw on paths that contain characters unrepresentable at the OS level,
156-
# or with invalid syntax on Windows (https://bugs.python.org/issue35306)
157-
try:
158-
return path.exists()
159-
except OSError:
160-
return False
161-
162155
# These look like paths but may not exist
163156
possible_paths = (
164157
absolutepath(get_file_part_from_node_id(arg))

‎src/_pytest/pathlib.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import atexit
22
import contextlib
3-
import errno
43
import fnmatch
54
import importlib.util
65
import itertools
@@ -798,7 +797,7 @@ def safe_exists(p: Path) -> bool:
798797
"""Like Path.exists(), but account for input arguments that might be too long (#11394)."""
799798
try:
800799
return p.exists()
801-
except OSError as e:
802-
if e.errno == errno.ENAMETOOLONG:
803-
return False
804-
raise
800+
except (ValueError, OSError):
801+
# ValueError: stat: path too long for Windows
802+
# OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
803+
return False

‎testing/test_pathlib.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,6 @@ def test_safe_exists(tmp_path: Path) -> None:
688688
Path,
689689
"exists",
690690
autospec=True,
691-
side_effect=OSError(errno.EIO, "another kind of error"),
691+
side_effect=ValueError("name too long"),
692692
):
693-
with pytest.raises(OSError):
694-
_ = safe_exists(p)
693+
assert safe_exists(p) is False

0 commit comments

Comments
 (0)
Please sign in to comment.