Skip to content

Commit

Permalink
hash get_object_state, use dir for native types, fix __slots__
Browse files Browse the repository at this point in the history
Fix #207
  • Loading branch information
albertz committed Oct 8, 2024
1 parent c7de85e commit f68ac61
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion sisyphus/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,19 @@ def get_object_state(obj):

if hasattr(obj, "__sis_state__"):
state = obj.__sis_state__()
elif getattr(obj, "__class__", None) and getattr(obj.__class__, "__dictoffset__", 0) > 0:
# This type has some native attribs, which are not in __dict__ (and neither in __slots__).
# `dir()` should be able to list those.
# One example is the native `_functools.partial`.
# https://github.com/rwth-i6/sisyphus/issues/207
return {k: getattr(obj, k) for k in dir(obj) if not k.startswith("__")}
elif hasattr(obj, "__getstate__"):
state = obj.__getstate__()
elif hasattr(obj, "__dict__"):
elif hasattr(obj, "__dict__") and not hasattr(obj, "__slots__"):
state = obj.__dict__
elif hasattr(obj, "__dict__") and hasattr(obj, "__slots__"):
state = obj.__dict__.copy()
state.update({k: getattr(obj, k) for k in obj.__slots__ if hasattr(obj, k)})
elif hasattr(obj, "__slots__"):
state = {k: getattr(obj, k) for k in obj.__slots__ if hasattr(obj, k)}
else:
Expand Down

0 comments on commit f68ac61

Please sign in to comment.