Skip to content

Commit 02ae86f

Browse files
authored
feat: Remove references to py_info from FileCache (#2948)
1 parent 03f4800 commit 02ae86f

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

docs/changelog/2074b.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove references to py_info in FileCache - by :user:`esafak`.

src/virtualenv/cache/file_cache.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, Callable
44

5-
from virtualenv.app_data.na import AppDataDisabled
65
from virtualenv.cache import Cache
76

87
if TYPE_CHECKING:
98
from pathlib import Path
109

11-
from virtualenv.app_data.base import AppData
10+
from virtualenv.app_data.base import ContentStore
1211

1312

1413
class FileCache(Cache):
15-
def __init__(self, app_data: AppData) -> None:
16-
self.app_data = app_data if app_data is not None else AppDataDisabled()
14+
def __init__(self, store_factory: Callable[[Path], ContentStore], clearer: Callable[[], None] | None) -> None:
15+
self.store_factory = store_factory
16+
self.clearer = clearer
1717

1818
def get(self, key: Path) -> dict | None:
1919
"""Get a value from the file cache."""
20-
py_info, py_info_store = None, self.app_data.py_info(key)
21-
with py_info_store.locked():
22-
if py_info_store.exists():
23-
py_info = py_info_store.read()
24-
return py_info
20+
result, store = None, self.store_factory(key)
21+
with store.locked():
22+
if store.exists():
23+
result = store.read()
24+
return result
2525

2626
def set(self, key: Path, value: dict) -> None:
2727
"""Set a value in the file cache."""
28-
py_info_store = self.app_data.py_info(key)
29-
with py_info_store.locked():
30-
py_info_store.write(value)
28+
store = self.store_factory(key)
29+
with store.locked():
30+
store.write(value)
3131

3232
def remove(self, key: Path) -> None:
3333
"""Remove a value from the file cache."""
34-
py_info_store = self.app_data.py_info(key)
35-
with py_info_store.locked():
36-
if py_info_store.exists():
37-
py_info_store.remove()
34+
store = self.store_factory(key)
35+
with store.locked():
36+
if store.exists():
37+
store.remove()
3838

3939
def clear(self) -> None:
4040
"""Clear the entire file cache."""
41-
self.app_data.py_info_clear()
41+
if self.clearer is not None:
42+
self.clearer()
4243

4344

4445
__all__ = [

src/virtualenv/discovery/cached_py_info.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def from_exe( # noqa: PLR0913
4646
) -> PythonInfo | None:
4747
env = os.environ if env is None else env
4848
if cache is None:
49-
cache = FileCache(app_data)
49+
if app_data is None:
50+
app_data = AppDataDisabled()
51+
cache = FileCache(store_factory=app_data.py_info, clearer=app_data.py_info_clear)
5052
result = _get_from_cache(cls, app_data, exe, env, cache, ignore_cache=ignore_cache)
5153
if isinstance(result, Exception):
5254
if raise_on_error:
@@ -201,7 +203,7 @@ def __repr__(self) -> str:
201203
def clear(app_data=None, cache=None):
202204
"""Clear the cache."""
203205
if cache is None and app_data is not None:
204-
cache = FileCache(app_data)
206+
cache = FileCache(store_factory=app_data.py_info, clearer=app_data.py_info_clear)
205207
if cache is not None:
206208
cache.clear()
207209
_CACHE.clear()

0 commit comments

Comments
 (0)