Skip to content

Commit 41101eb

Browse files
committed
fixup! refactor(core.utils): use platformdirs for default cache dir
Drop the inline _default_cache_dir() platform branching and call platformdirs.user_cache_path("cuda-python", appauthor=False) / "program-cache" instead. The inline version handled the obvious cases (XDG_CACHE_HOME on Linux, ~/Library/Caches on macOS, %LOCALAPPDATA% on Windows) but glossed over Windows FOLDERID_* / Known Folder semantics that platformdirs encodes correctly. Adds platformdirs >=3.0 as a runtime dep in both pyproject.toml and pixi.toml. Drops the platform-branch unit tests (we don't re-test platformdirs' logic) and keeps a thin "we land in the right user-cache root with the right leaf" assertion plus the end-to-end "default dir is used when path is omitted" integration test.
1 parent f7228a4 commit 41101eb

4 files changed

Lines changed: 24 additions & 90 deletions

File tree

cuda_core/cuda/core/utils/_program_cache.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import errno
2020
import hashlib
2121
import os
22-
import sys
2322
import tempfile
2423
import threading
2524
import time
2625
from pathlib import Path
26+
27+
import platformdirs
2728
from typing import Iterable, Sequence
2829

2930
from cuda.core._module import ObjectCode
@@ -831,36 +832,24 @@ def _probe(label: str, fn):
831832
_REPLACE_RETRY_DELAYS = (0.0, 0.005, 0.010, 0.020, 0.050, 0.100) # ~185ms budget
832833

833834

834-
# Sub-path appended to whatever OS-level cache root we discover. Vendor name
835-
# disambiguates from other "cuda-python" caches a user might have, and the
836-
# leaf names the artifact ("compiled programs") in case future tooling adds
837-
# sibling caches under the same vendor root.
838-
_DEFAULT_CACHE_DIR_LEAF = ("cuda-python", "program-cache")
839-
840-
841835
def _default_cache_dir() -> Path:
842836
"""OS-conventional default location for the file-stream cache.
843837
844-
* Linux / *BSD: ``$XDG_CACHE_HOME/cuda-python/program-cache``,
845-
defaulting to ``~/.cache/cuda-python/program-cache``.
846-
* macOS: ``~/Library/Caches/cuda-python/program-cache``.
847-
* Windows: ``%LOCALAPPDATA%\\cuda-python\\program-cache``,
848-
defaulting to ``~/AppData/Local/cuda-python/program-cache``.
838+
Resolves to the user-cache root for the calling user, with a
839+
``program-cache`` leaf so future tooling can place sibling caches
840+
under the same ``cuda-python`` vendor directory:
841+
842+
* Linux / *BSD: ``$XDG_CACHE_HOME/cuda-python/program-cache``
843+
(default ``~/.cache/cuda-python/program-cache``).
844+
* macOS: ``~/Library/Caches/cuda-python/program-cache``.
845+
* Windows: ``%LOCALAPPDATA%\\cuda-python\\program-cache``
846+
(Windows uses local AppData -- caches don't roam).
849847
850-
Implemented inline (no ``platformdirs`` dependency) since cuda_core
851-
keeps a deliberately small runtime dep set.
848+
Delegates to :mod:`platformdirs`, which encodes the per-platform
849+
rules canonically (and tracks corner cases like ``FOLDERID_*`` on
850+
Windows that an ad-hoc reimplementation would miss).
852851
"""
853-
if _IS_WINDOWS:
854-
base = os.environ.get("LOCALAPPDATA")
855-
if not base:
856-
base = str(Path.home() / "AppData" / "Local")
857-
return Path(base, *_DEFAULT_CACHE_DIR_LEAF)
858-
if sys.platform == "darwin":
859-
return Path.home() / "Library" / "Caches" / Path(*_DEFAULT_CACHE_DIR_LEAF)
860-
base = os.environ.get("XDG_CACHE_HOME")
861-
if not base:
862-
base = str(Path.home() / ".cache")
863-
return Path(base, *_DEFAULT_CACHE_DIR_LEAF)
852+
return platformdirs.user_cache_path("cuda-python", appauthor=False) / "program-cache"
864853

865854

866855
def _replace_with_sharing_retry(tmp_path: Path, target: Path) -> bool:

cuda_core/pixi.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ cuda-version = "*"
185185
numpy = "*"
186186
cuda-bindings = "*"
187187
cuda-pathfinder = "*"
188+
platformdirs = ">=3.0"
188189

189190
[target.linux.tasks.build-cython-tests]
190191
cmd = ["$PIXI_PROJECT_ROOT/tests/cython/build_tests.sh"]

cuda_core/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ classifiers = [
5050
dependencies = [
5151
"cuda-pathfinder >=1.4.2",
5252
"numpy",
53+
"platformdirs >=3.0",
5354
]
5455

5556
[project.optional-dependencies]

cuda_core/tests/test_program_cache.py

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,73 +1239,16 @@ def test_filestream_cache_rejects_negative_size_cap(tmp_path):
12391239
FileStreamProgramCache(tmp_path / "fc", max_size_bytes=-1)
12401240

12411241

1242-
@pytest.mark.parametrize(
1243-
"platform, env, home, expected",
1244-
[
1245-
pytest.param(
1246-
"linux",
1247-
{"XDG_CACHE_HOME": "/explicit/xdg"},
1248-
"/home/u",
1249-
"/explicit/xdg/cuda-python/program-cache",
1250-
id="linux_xdg_set",
1251-
),
1252-
pytest.param(
1253-
"linux",
1254-
{},
1255-
"/home/u",
1256-
"/home/u/.cache/cuda-python/program-cache",
1257-
id="linux_xdg_unset",
1258-
),
1259-
pytest.param(
1260-
"darwin",
1261-
{},
1262-
"/Users/u",
1263-
"/Users/u/Library/Caches/cuda-python/program-cache",
1264-
id="macos",
1265-
),
1266-
],
1267-
)
1268-
def test_default_cache_dir_posix(monkeypatch, platform, env, home, expected):
1269-
from pathlib import Path
1270-
1271-
from cuda.core.utils._program_cache import _default_cache_dir
1272-
1273-
monkeypatch.setattr("cuda.core.utils._program_cache._IS_WINDOWS", False)
1274-
monkeypatch.setattr("cuda.core.utils._program_cache.sys.platform", platform)
1275-
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
1276-
monkeypatch.delenv("LOCALAPPDATA", raising=False)
1277-
for k, v in env.items():
1278-
monkeypatch.setenv(k, v)
1279-
monkeypatch.setattr(Path, "home", classmethod(lambda cls: cls(home)))
1280-
1281-
assert _default_cache_dir() == Path(expected)
1282-
1283-
1284-
def test_default_cache_dir_windows_localappdata_set(monkeypatch):
1285-
from pathlib import Path
1242+
def test_default_cache_dir_lives_under_user_cache_root():
1243+
"""We delegate per-platform path resolution to ``platformdirs``, so we
1244+
only verify the cuda-python-owned suffix; the OS-specific bits are
1245+
platformdirs' responsibility."""
1246+
import platformdirs
12861247

12871248
from cuda.core.utils._program_cache import _default_cache_dir
12881249

1289-
monkeypatch.setattr("cuda.core.utils._program_cache._IS_WINDOWS", True)
1290-
monkeypatch.setattr("cuda.core.utils._program_cache.sys.platform", "win32")
1291-
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
1292-
monkeypatch.setenv("LOCALAPPDATA", "/fake/appdata/local")
1293-
1294-
assert _default_cache_dir() == Path("/fake/appdata/local/cuda-python/program-cache")
1295-
1296-
1297-
def test_default_cache_dir_windows_localappdata_unset_falls_back_to_home(monkeypatch):
1298-
from pathlib import Path
1299-
1300-
from cuda.core.utils._program_cache import _default_cache_dir
1301-
1302-
monkeypatch.setattr("cuda.core.utils._program_cache._IS_WINDOWS", True)
1303-
monkeypatch.setattr("cuda.core.utils._program_cache.sys.platform", "win32")
1304-
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
1305-
monkeypatch.delenv("LOCALAPPDATA", raising=False)
1306-
monkeypatch.setattr(Path, "home", classmethod(lambda cls: cls("/fake/userprofile")))
1307-
1308-
assert _default_cache_dir() == Path("/fake/userprofile/AppData/Local/cuda-python/program-cache")
1250+
expected = platformdirs.user_cache_path("cuda-python", appauthor=False) / "program-cache"
1251+
assert _default_cache_dir() == expected
13091252

13101253

13111254
def test_filestream_cache_uses_default_dir_when_path_omitted(tmp_path, monkeypatch):

0 commit comments

Comments
 (0)