Skip to content

Commit ca74715

Browse files
committed
correction
1 parent 8590f2f commit ca74715

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/pythonfinder/utils/path_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ def exists_and_is_accessible(path: Path) -> bool:
238238
"""
239239
try:
240240
return path.exists()
241-
except PermissionError:
242-
# Treat any permission-denied error (including Windows WinError 5) as
243-
# inaccessible rather than crashing.
244-
return False
245-
except OSError:
246-
# Catch other OS-level errors (e.g. Windows ERROR_ACCESS_DENIED variants
247-
# that may surface as OSError rather than PermissionError).
248-
return False
241+
except PermissionError as error:
242+
if error.errno == errno.EACCES or getattr(error, "winerror", None) == 5:
243+
return False
244+
raise
245+
except OSError as error:
246+
if error.errno == errno.EACCES or getattr(error, "winerror", None) == 5:
247+
return False
248+
raise
249249

250250

251251
def is_in_path(path: str | Path, parent_path: str | Path) -> bool:

tests/test_path_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ def test_exists_and_is_accessible():
241241
):
242242
exists_and_is_accessible(Path("/usr/bin/python"))
243243

244+
class WindowsAccessDenied(OSError):
245+
def __init__(self):
246+
super().__init__("Access is denied")
247+
self.winerror = 5
248+
self.errno = None
249+
250+
with mock.patch("pathlib.Path.exists", side_effect=WindowsAccessDenied()):
251+
assert not exists_and_is_accessible(Path("/usr/bin/python"))
252+
253+
with pytest.raises(OSError):
254+
with mock.patch("pathlib.Path.exists", side_effect=OSError(1, "Other error")):
255+
exists_and_is_accessible(Path("/usr/bin/python"))
256+
244257

245258
def test_is_in_path():
246259
"""Test that is_in_path correctly checks if a path is inside another path."""

0 commit comments

Comments
 (0)