From 31cbaef29e718ea451abbac3e3ce6b649ccd7e5e Mon Sep 17 00:00:00 2001 From: Albert Zeyer Date: Thu, 10 Oct 2024 18:41:41 +0200 Subject: [PATCH] explicit state/hash for pathlib paths --- sisyphus/hash.py | 8 ++++++++ tests/hash_unittest.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/sisyphus/hash.py b/sisyphus/hash.py index 7a85479..752963d 100644 --- a/sisyphus/hash.py +++ b/sisyphus/hash.py @@ -1,5 +1,6 @@ import enum import hashlib +import pathlib from inspect import isclass, isfunction, ismemberdescriptor @@ -43,6 +44,13 @@ def get_object_state(obj): Comment: Maybe obj.__reduce__() is a better idea? is it stable for hashing? """ + if isinstance(obj, pathlib.PurePath): + # pathlib paths have a somewhat technical internal state + # ('_drv', '_root', '_parts', '_str', '_hash', '_pparts', '_cached_cparts'), + # so we don't want to rely on this, but instead just use the string representation as state. + # https://github.com/rwth-i6/sisyphus/pull/208#issuecomment-2405560718 + return str(obj) + if hasattr(obj, "__getnewargs_ex__"): args = obj.__getnewargs_ex__() elif hasattr(obj, "__getnewargs__"): diff --git a/tests/hash_unittest.py b/tests/hash_unittest.py index f23b618..ad804f2 100644 --- a/tests/hash_unittest.py +++ b/tests/hash_unittest.py @@ -47,6 +47,12 @@ def test_functools_partial(self): ), ) + def test_pathlib_Path(self): + from pathlib import Path + + obj = Path("/etc/passwd") + self.assertEqual(sis_hash_helper(obj), b"(PosixPath, (str, '/etc/passwd'))") + if __name__ == "__main__": unittest.main()