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
2 changes: 2 additions & 0 deletions docs/changelog/+skip-path-dir-probe.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`~python_discovery.iter_interpreters` with no key no longer probes ``PATH`` directories or subdirectories named
like Python executables as interpreters.
5 changes: 3 additions & 2 deletions src/python_discovery/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,13 @@ def path_exe_finder(
direct = f"{direct}.exe"

def path_exes(path: Path) -> Generator[tuple[Path, bool], None, None]:
# an empty spec (enumerating everything) would make the direct path the PATH directory itself
direct_path = path / direct
if direct_path.exists():
if direct_path.is_file():
yield direct_path, impl_must_match

for exe in path.iterdir():
if match := pat.fullmatch(exe.name):
if (match := pat.fullmatch(exe.name)) and exe.is_file():
yield exe.absolute(), impl_must_match or match["impl"].lower() == "python"

return path_exes
Expand Down
24 changes: 24 additions & 0 deletions tests/test_iter_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ def test_iter_interpreters_no_key_probes_variant_builds_on_path(
assert str(exe) in [call.args[0] for call in from_exe.call_args_list]


@pytest.mark.usefixtures("uv_dir")
@pytest.mark.parametrize(
"directory_name",
[
pytest.param(".", id="path-directory"),
pytest.param(f"python3.99{'.exe' if IS_WIN else ''}", id="named-directory"),
],
)
def test_iter_interpreters_skips_path_directories(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture, directory_name: str
) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
(bin_dir / directory_name).mkdir(exist_ok=True)
executable = bin_dir / f"python3.98{'.exe' if IS_WIN else ''}"
executable.touch()
monkeypatch.setenv("PATH", str(bin_dir))
from_exe = mocker.patch("python_discovery._discovery.PathPythonInfo.from_exe", return_value=None)

list(iter_interpreters())

assert [call.args[0] for call in from_exe.call_args_list] == [str(executable)]


@pytest.mark.usefixtures("uv_dir")
def test_iter_interpreters_dedups_symlinks(
tmp_path: Path,
Expand Down
Loading