Skip to content

Commit de1820d

Browse files
fix: Patch get_interpreter to handle missing cache and app_data (#2974)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8e1ecc7 commit de1820d

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

docs/changelog/2972.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Patch get_interpreter to handle missing cache and app_data - by :user:`esafak`

src/virtualenv/discovery/builtin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ def get_interpreter(
7777
cache=None,
7878
env: Mapping[str, str] | None = None,
7979
) -> PythonInfo | None:
80+
"""
81+
Find an interpreter that matches a given specification.
82+
83+
:param key: the specification of the interpreter to find
84+
:param try_first_with: a list of interpreters to try first
85+
:param app_data: the application data folder
86+
:param cache: a cache of python information
87+
:param env: the environment to use
88+
:return: the interpreter if found, otherwise None
89+
"""
90+
if cache is None:
91+
# Import locally to avoid a circular dependency
92+
from virtualenv.app_data import AppDataDisabled # noqa: PLC0415
93+
from virtualenv.cache import FileCache # noqa: PLC0415
94+
95+
if app_data is None:
96+
app_data = AppDataDisabled()
97+
cache = FileCache(store_factory=app_data.py_info, clearer=app_data.py_info_clear)
98+
8099
spec = PythonSpec.from_string_spec(key)
81100
LOGGER.info("find interpreter for spec %r", spec)
82101
proposed_paths = set()

tests/unit/discovery/test_discovery.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ def test_returns_second_python_specified_when_more_than_one_is_specified_and_env
224224
assert result == mocker.sentinel.python_from_cli
225225

226226

227+
def test_get_interpreter_no_cache_no_app_data():
228+
"""Test that get_interpreter can be called without cache and app_data."""
229+
# A call to a valid interpreter should succeed and return a PythonInfo object.
230+
interpreter = get_interpreter(sys.executable, [])
231+
assert interpreter is not None
232+
assert Path(interpreter.executable).is_file()
233+
234+
# A call to an invalid interpreter should not fail and should return None.
235+
interpreter = get_interpreter("a-python-that-does-not-exist", [])
236+
assert interpreter is None
237+
238+
227239
def test_discovery_absolute_path_with_try_first(tmp_path):
228240
good_env = tmp_path / "good"
229241
bad_env = tmp_path / "bad"

0 commit comments

Comments
 (0)