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/2467.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix cache invalidation for PythonInfo by hashing `py_info.py`.
Contributed by :user:`esafak`.
15 changes: 13 additions & 2 deletions src/virtualenv/discovery/cached_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

import hashlib
import logging
import os
import random
Expand Down Expand Up @@ -58,14 +59,23 @@ def _get_via_file_cache(cls, app_data, path, exe, env):
path_modified = path.stat().st_mtime
except OSError:
path_modified = -1
py_info_script = Path(os.path.abspath(__file__)).parent / "py_info.py"
try:
py_info_hash = hashlib.sha256(py_info_script.read_bytes()).hexdigest()
except OSError:
py_info_hash = None

if app_data is None:
app_data = AppDataDisabled()
py_info, py_info_store = None, app_data.py_info(path)
with py_info_store.locked():
if py_info_store.exists(): # if exists and matches load
data = py_info_store.read()
of_path, of_st_mtime, of_content = data["path"], data["st_mtime"], data["content"]
if of_path == path_text and of_st_mtime == path_modified:
of_path = data.get("path")
of_st_mtime = data.get("st_mtime")
of_content = data.get("content")
of_hash = data.get("hash")
if of_path == path_text and of_st_mtime == path_modified and of_hash == py_info_hash:
py_info = cls._from_dict(of_content.copy())
sys_exe = py_info.system_executable
if sys_exe is not None and not os.path.exists(sys_exe):
Expand All @@ -80,6 +90,7 @@ def _get_via_file_cache(cls, app_data, path, exe, env):
"st_mtime": path_modified,
"path": path_text,
"content": py_info._to_dict(), # noqa: SLF001
"hash": py_info_hash,
}
py_info_store.write(data)
else:
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/discovery/py_info/test_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import pytest

from virtualenv.create.via_global_ref.builtin.cpython.common import is_macos_brew
from virtualenv.discovery import cached_py_info
from virtualenv.discovery.py_info import PythonInfo, VersionInfo
from virtualenv.discovery.py_spec import PythonSpec
Expand Down Expand Up @@ -154,6 +155,36 @@ def test_py_info_cache_clear(mocker, session_app_data):
assert spy.call_count >= 2 * count


def test_py_info_cache_invalidation_on_py_info_change(mocker, session_app_data):
# 1. Get a PythonInfo object for the current executable, this will cache it.
PythonInfo.from_exe(sys.executable, session_app_data)

# 2. Spy on _run_subprocess
spy = mocker.spy(cached_py_info, "_run_subprocess")

# 3. Modify the content of py_info.py
py_info_script = Path(cached_py_info.__file__).parent / "py_info.py"
original_content = py_info_script.read_text(encoding="utf-8")

try:
# 4. Clear the in-memory cache
mocker.patch.dict(cached_py_info._CACHE, {}, clear=True) # noqa: SLF001
py_info_script.write_text(original_content + "\n# a comment", encoding="utf-8")

# 5. Get the PythonInfo object again
info = PythonInfo.from_exe(sys.executable, session_app_data)

# 6. Assert that _run_subprocess was called again
if is_macos_brew(info):
assert spy.call_count in {2, 3}
else:
assert spy.call_count == 2

finally:
# Restore the original content
py_info_script.write_text(original_content, encoding="utf-8")


@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink is not supported")
@pytest.mark.xfail(
# https://doc.pypy.org/en/latest/install.html?highlight=symlink#download-a-pre-built-pypy
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/discovery/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def test_absolute_path_does_not_exist(tmp_path):
capture_output=True,
text=True,
check=False,
encoding="utf-8",
)

# Check that the command was successful
Expand All @@ -283,6 +284,7 @@ def test_absolute_path_does_not_exist_fails(tmp_path):
capture_output=True,
text=True,
check=False,
encoding="utf-8",
)

# Check that the command failed
Expand Down
Loading