From 691e0c3c2b328e670210fe23371a9dc9e4606679 Mon Sep 17 00:00:00 2001 From: Albert Zeyer Date: Fri, 11 Oct 2024 12:36:23 +0200 Subject: [PATCH] extract_paths, use get_object_state This fixes the same problems as #207 but now for extract_paths, by using the same shared code (get_object_state). --- sisyphus/hash.py | 8 ++++++-- sisyphus/tools.py | 18 ++++-------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/sisyphus/hash.py b/sisyphus/hash.py index 752963d..0b258c0 100644 --- a/sisyphus/hash.py +++ b/sisyphus/hash.py @@ -58,14 +58,18 @@ def get_object_state(obj): else: args = None - if hasattr(obj, "__sis_state__"): + if hasattr(obj, "__sis_state__") and not isinstance(obj, type): state = obj.__sis_state__() # Note: Since Python 3.11, there is a default object.__getstate__. # However, this default object.__getstate__ is not correct for some native types, e.g. _functools.partial. # https://github.com/rwth-i6/sisyphus/issues/207 # https://github.com/python/cpython/issues/125094 # Thus, only use __getstate__ if it is not the default object.__getstate__. - elif hasattr(obj, "__getstate__") and obj.__class__.__getstate__ is not getattr(object, "__getstate__", None): + elif ( + hasattr(obj, "__getstate__") + and obj.__class__.__getstate__ is not getattr(object, "__getstate__", None) + and not isinstance(obj, type) + ): state = obj.__getstate__() else: state = _getmembers(obj) diff --git a/sisyphus/tools.py b/sisyphus/tools.py index 32f9777..30ba22b 100644 --- a/sisyphus/tools.py +++ b/sisyphus/tools.py @@ -23,6 +23,7 @@ import sisyphus.global_settings as gs from sisyphus.block import Block +from sisyphus.hash import get_object_state def get_system_informations(file=sys.stdout): @@ -88,25 +89,14 @@ def extract_paths(args: Any) -> Set: continue if hasattr(obj, "_sis_path") and obj._sis_path is True and not type(obj) is type: out.add(obj) - elif isinstance(obj, (list, tuple, set)): + elif isinstance(obj, (list, tuple, set, frozenset)): queue.extend(obj) elif isinstance(obj, dict): for k, v in obj.items(): if not type(k) == str or not k.startswith("_sis_"): queue.append(v) - elif hasattr(obj, "__sis_state__") and not inspect.isclass(obj): - queue.append(obj.__sis_state__()) - elif hasattr(obj, "__getstate__") and not inspect.isclass(obj): - queue.append(obj.__getstate__()) - elif hasattr(obj, "__dict__"): - for k, v in obj.__dict__.items(): - if not type(k) == str or not k.startswith("_sis_"): - queue.append(v) - elif hasattr(obj, "__slots__"): - for k in obj.__slots__: - if hasattr(obj, k) and not k.startswith("_sis_"): - a = getattr(obj, k) - queue.append(a) + else: + queue.append(get_object_state(obj)) return out