Skip to content
Open
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
2 changes: 2 additions & 0 deletions docs/changelog/+unnameable-working-directory.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`~python_discovery.get_interpreter` no longer raises while resolving a ``pyenv`` shim when the working
directory was removed, or on illumos sits below an unreadable directory; it reads ``$PWD`` instead, as ``pyenv`` does.
7 changes: 6 additions & 1 deletion src/python_discovery/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ def _active_versions(env: Mapping[str, str]) -> Generator[str, None, None]:
if pyenv_version := env.get("PYENV_VERSION"):
yield from pyenv_version.split(":")
return
if versions := _read_python_version_file(Path.cwd()):
cwd: str | Path | None
try:
cwd = Path.cwd()
except OSError: # removed, or on illumos below an unreadable directory; pyenv reads $PWD as well
cwd = env.get("PWD")
if cwd and (versions := _read_python_version_file(cwd)):
yield from versions
return
if (pyenv_root := env.get("PYENV_ROOT")) and (
Expand Down
19 changes: 19 additions & 0 deletions tests/python_discovery/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,32 @@ def test_shim_unreadable_python_version_file_is_skipped(
assert pyenv_probed() == ("versions", "2.7.18")


@pytest.mark.parametrize("error", [FileNotFoundError, PermissionError])
def test_shim_unnameable_working_directory_reads_pwd(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
mocker: MockerFixture,
pyenv_probed: Callable[[], tuple[str, ...]],
error: type[OSError],
) -> None:
root = Path(os.environ["PYENV_ROOT"])
_create_versioned_binary(root, ("versions",), "2.7.15", "python2.7")
(root / "version").write_text("2.7.15\n", encoding="utf-8")
(tmp_path / ".python-version").write_text("2.7.18\n", encoding="utf-8")
monkeypatch.setenv("PWD", str(tmp_path / "removed"))
mocker.patch.object(Path, "cwd", autospec=True, side_effect=error)

assert pyenv_probed() == ("versions", "2.7.18")


@_POSIX_PERMISSIONS
def test_shim_unsearchable_directory_is_skipped(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, pyenv_probed: Callable[[], tuple[str, ...]]
) -> None:
(tmp_path / ".python-version").write_text("2.7.18\n", encoding="utf-8")
(work := tmp_path / "locked" / "work").mkdir(parents=True)
monkeypatch.chdir(work)
monkeypatch.setenv("PWD", str(work))
work.parent.chmod(0o600)
try:
assert pyenv_probed() == ("versions", "2.7.18")
Expand Down