Skip to content

Commit

Permalink
#4 hash __self__ when calculating function hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlin-mladenov committed Apr 17, 2017
1 parent 9e34264 commit ad5c9e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion fcache/fcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def decorated(*args, **kwargs):
f_closure_values = tuple(map(lambda c: c.cell_contents, f.__closure__))
else:
f_closure_values = None
call_hash = stable_hash((f, args, kwargs, f_closure_values))
bounded_obj = getattr(f, '__self__', None)
call_hash = stable_hash((f, args, kwargs, f_closure_values, bounded_obj))
if call_hash in cache:
return cache[call_hash]
else:
Expand Down
20 changes: 19 additions & 1 deletion fcache/tests/test_caching_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,25 @@ def get():
assert cached_res == get_res
assert cached_res is not get_res

# TODO Test class function

# Test class function
class SimpleClass:
def __init__(self):
self.n = 0
def inc(self):
self.n += 1
def get(self):
return self.n

def test_bound_method():
obj = SimpleClass()
fun = fcache(obj.get)
assert fun() == 0
obj.inc()
obj.inc()
assert fun() == 2


# TODO Test well behaived

# Test with numpy and pandas - in a separate file

0 comments on commit ad5c9e6

Please sign in to comment.