Skip to content

Commit

Permalink
PersistentDict: honor XDG_CACHE_HOME on MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener committed Mar 19, 2024
1 parent da5d9e6 commit 6d49325
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,15 @@ def __init__(self, identifier, key_builder=None, container_dir=None):
except ImportError:
import appdirs

if sys.platform == "darwin" and os.getenv("XDG_CACHE_HOME") is not None:
# appdirs and platformdirs do not handle XDG_CACHE_HOME on macOS
# https://github.com/platformdirs/platformdirs/issues/269
cache_dir = join(os.getenv("XDG_CACHE_HOME"), "pytools")
else:
cache_dir = appdirs.user_cache_dir("pytools", "pytools")

container_dir = join(
appdirs.user_cache_dir("pytools", "pytools"),
cache_dir,
"pdict-v4-{}-py{}".format(
identifier,
".".join(str(i) for i in sys.version_info)))
Expand Down
22 changes: 22 additions & 0 deletions pytools/test/test_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,28 @@ class MyAttrs2:
assert keyb(MyAttrs2("hi", 1)) != keyb(MyAttrs("hi", 1))


def test_xdg_cache_home():
import os
xdg_dir = "tmpdir_pytools_xdg_test"

assert not os.path.exists(xdg_dir)

try:
old_xdg_cache_home = os.getenv("XDG_CACHE_HOME")
os.environ["XDG_CACHE_HOME"] = xdg_dir

PersistentDict("pytools-test")

assert os.path.exists(xdg_dir)
finally:
if old_xdg_cache_home is not None:
os.environ["XDG_CACHE_HOME"] = old_xdg_cache_home
else:
del os.environ["XDG_CACHE_HOME"]

shutil.rmtree(xdg_dir)


if __name__ == "__main__":
if len(sys.argv) > 1:
exec(sys.argv[1])
Expand Down

0 comments on commit 6d49325

Please sign in to comment.