Skip to content

Commit

Permalink
fix infinite recursion, more consistent type check
Browse files Browse the repository at this point in the history
  • Loading branch information
albertz committed Oct 18, 2024
1 parent 681a36e commit b504060
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions sisyphus/hash.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Tuple
import enum
import hashlib
import pathlib
Expand Down Expand Up @@ -37,6 +38,12 @@ def short_hash(obj, length=12, chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef
return "".join(ls)


_BasicTypes: Tuple[type, ...] = (int, float, bool, str, complex)
_BasicSeqTypes: Tuple[type, ...] = (list, tuple)
_BasicSetTypes: Tuple[type, ...] = (set, frozenset)
_BasicDictTypes: Tuple[type, ...] = (dict,)


def get_object_state(obj):
"""
Export current object status
Expand All @@ -49,7 +56,7 @@ def get_object_state(obj):
# so we keep consistent to the behavior of sis_hash_helper.
if obj is None:
return None
if isinstance(obj, (bool, int, float, complex, str)):
if isinstance(obj, _BasicTypes + _BasicSeqTypes + _BasicSetTypes + _BasicDictTypes):
return obj
if isfunction(obj) or isclass(obj):
return obj.__module__, obj.__qualname__
Expand Down Expand Up @@ -111,11 +118,11 @@ def sis_hash_helper(obj):
byte_list.append(obj)
elif obj is None:
pass
elif type(obj) in (int, float, bool, str, complex):
elif isinstance(obj, _BasicTypes):
byte_list.append(repr(obj).encode())
elif type(obj) in (list, tuple):
elif isinstance(obj, _BasicSeqTypes):
byte_list += map(sis_hash_helper, obj)
elif type(obj) in (set, frozenset):
elif isinstance(obj, _BasicDictTypes):
byte_list += sorted(map(sis_hash_helper, obj))
elif isinstance(obj, dict):
# sort items to ensure they are always in the same order
Expand Down

0 comments on commit b504060

Please sign in to comment.