From 317e23d855cdf09a8a2411dcbc4508c31ef9c989 Mon Sep 17 00:00:00 2001 From: hwdev Date: Fri, 13 Nov 2026 17:52:15 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(discovery):=20read=20$PWD=20?= =?UTF-8?q?when=20the=20working=20directory=20can't=20be=20named?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolving a pyenv shim looks for `.python-version` starting from `Path.cwd()`. That raises when the working directory was removed (FileNotFoundError) or, on illumos, when an ancestor can't be read (PermissionError), so `get_interpreter` failed (#255). Fall back to `PWD` from the environment, pyenv's default start for its own `.python-version` search, so a local pin still wins over the global version. The unsearchable directory test now sets `PWD`, so it also passes where getcwd fails. --- .../+unnameable-working-directory.bugfix.rst | 2 ++ src/python_discovery/_discovery.py | 7 ++++++- tests/python_discovery/test_discovery.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 docs/changelog/+unnameable-working-directory.bugfix.rst diff --git a/docs/changelog/+unnameable-working-directory.bugfix.rst b/docs/changelog/+unnameable-working-directory.bugfix.rst new file mode 100644 index 0000000..578af01 --- /dev/null +++ b/docs/changelog/+unnameable-working-directory.bugfix.rst @@ -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. diff --git a/src/python_discovery/_discovery.py b/src/python_discovery/_discovery.py index 6e58f7f..5a8abbd 100644 --- a/src/python_discovery/_discovery.py +++ b/src/python_discovery/_discovery.py @@ -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 ( diff --git a/tests/python_discovery/test_discovery.py b/tests/python_discovery/test_discovery.py index 0d5de82..f754846 100644 --- a/tests/python_discovery/test_discovery.py +++ b/tests/python_discovery/test_discovery.py @@ -698,6 +698,24 @@ 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, ...]] @@ -705,6 +723,7 @@ def test_shim_unsearchable_directory_is_skipped( (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")