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/216.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retry interpreter discovery without the cache when cache access fails.
3 changes: 3 additions & 0 deletions docs/explanation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ with `filelock <https://py-filelock.readthedocs.io/>`_-based locking for safe co
can also pass ``cache=None`` to disable caching, or implement your own backend (see
:doc:`/how-to/standalone-usage`).

If cache access raises an OS error, discovery retries the interpreter query without that cache. A failed cache write
can therefore cause a second query.

On POSIX, ``DiskCache`` uses a cache root only when it and both ``py_info`` directories are real directories owned by
the current user or root and cannot be written by other users. Its ancestor directories must also prevent other users
from replacing the cache root; a sticky shared ancestor such as ``/tmp`` is allowed. Discovery continues without disk
Expand Down
9 changes: 8 additions & 1 deletion src/python_discovery/_cached_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ def from_exe( # ruff:ignore[too-many-arguments]
executable_args: str | None = None,
) -> PythonInfo | None:
env = os.environ if env is None else env
result = _get_from_cache(cls, cache, exe, env, ignore_cache=ignore_cache, executable_args=executable_args)
if cache is None:
result = _get_from_cache(cls, cache, exe, env, ignore_cache=ignore_cache, executable_args=executable_args)
else:
try:
result = _get_from_cache(cls, cache, exe, env, ignore_cache=ignore_cache, executable_args=executable_args)
except OSError:
_LOGGER.debug("failed to use cache for %s", exe, exc_info=True)
result = _get_from_cache(cls, None, exe, env, ignore_cache=True, executable_args=executable_args)
if isinstance(result, Exception):
if raise_on_error:
raise result
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cached_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,27 @@ def test_from_exe_cache_entry_with_bad_content(tmp_path: Path) -> None:
assert isinstance(store.read(), dict)


@pytest.mark.parametrize(
("folder", "suffix"),
[
pytest.param(".", ".json", id="entry"),
pytest.param("locks-v2" if os.name == "posix" else ".", ".lock", id="lock"),
],
)
def test_from_exe_recovers_unusable_cache_path(tmp_path: Path, folder: str, suffix: str) -> None:
cache = DiskCache(tmp_path)
expected = PythonInfo.from_exe(sys.executable, cache, ignore_cache=True, resolve_to_host=False)
assert expected is not None
entry = next(tmp_path.rglob("*.json"))
blocked = entry.parent / folder / f"{entry.stem}{suffix}"
blocked.unlink(missing_ok=True)
blocked.mkdir()

result = PythonInfo.from_exe(sys.executable, cache, ignore_cache=True, resolve_to_host=False)
assert result is not None
assert result.version_info == expected.version_info


_LAUNCHER: Final[str] = r"C:\Program Files\Python Launcher\py.exe"
_ARGS: Final[str] = '-3.11 --config "settings folder\\python.json"'
_SELECTED: Final[str] = r"C:\Python311\python.exe"
Expand Down