Use _pytest.pathlib.safe_exists in get_dirs_from_args · pytest-dev/pytest@63b0c6f (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@
57 57 from _pytest.pathlib import import_path
58 58 from _pytest.pathlib import ImportMode
59 59 from _pytest.pathlib import resolve_package_path
60 +from _pytest.pathlib import safe_exists
60 61 from _pytest.stash import Stash
61 62 from _pytest.warning_types import PytestConfigWarning
62 63 from _pytest.warning_types import warn_explicit_for
@@ -557,12 +558,8 @@ def _set_initial_conftests(
557 558 anchor = absolutepath(current / path)
558 559
559 560 # Ensure we do not break if what appears to be an anchor
560 -# is in fact a very long option (#10169).
561 -try:
562 -anchor_exists = anchor.exists()
563 -except OSError: # pragma: no cover
564 -anchor_exists = False
565 -if anchor_exists:
561 +# is in fact a very long option (#10169, #11394).
562 +if safe_exists(anchor):
566 563 self._try_load_conftest(anchor, importmode, rootpath)
567 564 foundanchor = True
568 565 if not foundanchor:
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
16 16 from _pytest.outcomes import fail
17 17 from _pytest.pathlib import absolutepath
18 18 from _pytest.pathlib import commonpath
19 +from _pytest.pathlib import safe_exists
19 20
20 21 if TYPE_CHECKING:
21 22 from . import Config
@@ -151,14 +152,6 @@ def get_dir_from_path(path: Path) -> Path:
151 152 return path
152 153 return path.parent
153 154
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 -
162 155 # These look like paths but may not exist
163 156 possible_paths = (
164 157 absolutepath(get_file_part_from_node_id(arg))
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
1 1 import atexit
2 2 import contextlib
3 -import errno
4 3 import fnmatch
5 4 import importlib.util
6 5 import itertools
@@ -798,7 +797,7 @@ def safe_exists(p: Path) -> bool:
798 797 """Like Path.exists(), but account for input arguments that might be too long (#11394)."""
799 798 try:
800 799 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
Original file line number Diff line number Diff line change
@@ -688,7 +688,6 @@ def test_safe_exists(tmp_path: Path) -> None:
688 688 Path,
689 689 "exists",
690 690 autospec=True,
691 -side_effect=OSError(errno.EIO, "another kind of error"),
691 +side_effect=ValueError("name too long"),
692 692 ):
693 -with pytest.raises(OSError):
694 -_ = safe_exists(p)
693 +assert safe_exists(p) is False