Skip to content

Commit 0a19378

Browse files
committed
hash bound method, more
1 parent b7f2b7e commit 0a19378

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

sisyphus/hash.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ def get_object_state(obj):
7474
raise TypeError(f"derived type {obj!r} {type(obj)!r} not handled yet")
7575
if isfunction(obj) or isclass(obj):
7676
return obj.__module__, obj.__qualname__
77-
if ismethod(obj):
78-
return get_object_state(obj.__self__), get_object_state(obj.__func__)
7977

8078
if isinstance(obj, pathlib.PurePath):
8179
# pathlib paths have a somewhat technical internal state
@@ -201,7 +199,8 @@ def _getmembers(obj):
201199
if getattr(cls, "__dict__", None):
202200
cls_dict.update(cls.__dict__)
203201
for key in dir(obj):
204-
if key.startswith("__"):
202+
# for bound methods, need to keep __self__ and __func__.
203+
if key.startswith("__") and key not in ["__self__", "__func__"]:
205204
continue
206205
if key in res:
207206
continue

tests/hash_unittest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,21 @@ def test_functools_partial(self):
7373
def test_bound_method(self):
7474
first_obj = MyFoo("First")
7575
second_obj = MyFoo("Second")
76-
self.assertNotEqual(sis_hash_helper(first_obj.get_data), sis_hash_helper(second_obj.get_data))
76+
func_hash = sis_hash_helper(MyFoo.get_data)
77+
bound_to_first_obj_hash = sis_hash_helper(first_obj.get_data)
78+
bound_to_second_obj_hash = sis_hash_helper(second_obj.get_data)
79+
80+
self.assertEqual(func_hash, b"(function, (tuple, (str, 'tests.hash_unittest'), (str, 'MyFoo.get_data')))")
81+
self.assertEqual(
82+
bound_to_first_obj_hash,
83+
(
84+
b"(method, (dict, "
85+
b"(tuple, (str, '__func__'), " + func_hash + b"), "
86+
b"(tuple, (str, '__self__'), (MyFoo, (dict, (tuple, (str, 'some_data'), (str, 'First')))))"
87+
b"))"
88+
),
89+
)
90+
self.assertNotEqual(bound_to_first_obj_hash, bound_to_second_obj_hash)
7791

7892
def test_pathlib_Path(self):
7993
from pathlib import Path

0 commit comments

Comments
 (0)