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
1 change: 1 addition & 0 deletions docs/changelog/219.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop repeated interpreter queries when a virtual environment reports the same base prefix with different path spellings.
6 changes: 4 additions & 2 deletions src/python_discovery/_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
from collections import OrderedDict
from itertools import product
from os.path import normcase, realpath
from string import digits
from typing import TYPE_CHECKING, ClassVar, Final, NamedTuple, TypedDict

Expand Down Expand Up @@ -462,7 +463,8 @@ def resolve_to_system(cls, cache: PyInfoCache | None, target: PythonInfo) -> Pyt
prefixes = OrderedDict()
while target.system_executable is None:
prefix = target.real_prefix or target.base_prefix or target.prefix
if prefix in prefixes:
prefix_key = normcase(realpath(prefix))
if prefix_key in prefixes:
if len(prefixes) == 1:
_LOGGER.info("%r links back to itself via prefixes", target)
target.system_executable = target.executable
Expand All @@ -472,7 +474,7 @@ def resolve_to_system(cls, cache: PyInfoCache | None, target: PythonInfo) -> Pyt
_LOGGER.error("%d: prefix=%s, info=%r", len(prefixes) + 1, prefix, target)
msg = "prefixes are causing a circle {}".format("|".join(prefixes.keys()))
raise RuntimeError(msg)
prefixes[prefix] = target
prefixes[prefix_key] = target
target = target.discover_exe(cache, prefix=prefix, exact=False)
if target.executable != target.system_executable:
resolved = cls.from_exe(target.system_executable, cache)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_py_info_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ def test_resolve_to_system_single_prefix_self_link(mocker: MockerFixture, caplog
assert any("links back to itself" in r.message for r in caplog.records)


def test_resolve_to_system_repeated_prefix_with_parent_segments(tmp_path: Path, mocker: MockerFixture) -> None:
(tmp_path / "child").mkdir()
target = PythonInfo()
target.system_executable = target.real_prefix = None
target.base_prefix = str(tmp_path)
repeated = PythonInfo()
repeated.system_executable = repeated.real_prefix = None
repeated.base_prefix = str(tmp_path / "child" / "..")
mocker.patch.object(PythonInfo, "discover_exe", return_value=repeated)

result = PythonInfo.resolve_to_system(None, target)

assert result.system_executable == repeated.executable


def test_discover_exe_cache_hit() -> None:
info = copy.deepcopy(CURRENT)
cached = copy.deepcopy(CURRENT)
Expand Down