Skip to content

Commit

Permalink
extract_paths, use get_object_state
Browse files Browse the repository at this point in the history
This fixes the same problems as #207
but now for extract_paths,
by using the same shared code (get_object_state).
  • Loading branch information
albertz committed Oct 11, 2024
1 parent 9e597db commit 691e0c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
8 changes: 6 additions & 2 deletions sisyphus/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 4 additions & 14 deletions sisyphus/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 691e0c3

Please sign in to comment.