Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/pythonfinder/finders/system_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ def __init__(
venv = os.environ.get("VIRTUAL_ENV")
if venv:
bin_dir = "Scripts" if os.name == "nt" else "bin"
venv_path = Path(venv).resolve() / bin_dir
try:
venv_path = Path(venv).resolve() / bin_dir
except (PermissionError, OSError):
# resolve() can raise PermissionError on Windows for restricted
# system directories; fall back to a non-resolving join.
venv_path = Path(venv) / bin_dir

# For Windows tests with Unix-style paths
if os.name == "nt" and str(venv).startswith("/"):
Expand Down
11 changes: 7 additions & 4 deletions src/pythonfinder/utils/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,14 @@ def exists_and_is_accessible(path: Path) -> bool:
"""
try:
return path.exists()
except PermissionError as pe:
if pe.errno == errno.EACCES: # Permission denied
except PermissionError as error:
if error.errno == errno.EACCES or getattr(error, "winerror", None) == 5:
return False
else:
raise
raise
except OSError as error:
if error.errno == errno.EACCES or getattr(error, "winerror", None) == 5:
return False
raise


def is_in_path(path: str | Path, parent_path: str | Path) -> bool:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ def test_exists_and_is_accessible():
):
exists_and_is_accessible(Path("/usr/bin/python"))

class WindowsAccessDenied(OSError):
def __init__(self):
super().__init__("Access is denied")
self.winerror = 5
self.errno = None

with mock.patch("pathlib.Path.exists", side_effect=WindowsAccessDenied()):
assert not exists_and_is_accessible(Path("/usr/bin/python"))

with pytest.raises(OSError):
with mock.patch("pathlib.Path.exists", side_effect=OSError(1, "Other error")):
exists_and_is_accessible(Path("/usr/bin/python"))


def test_is_in_path():
"""Test that is_in_path correctly checks if a path is inside another path."""
Expand Down
Loading