Skip to content

Commit

Permalink
Fix self reference to weak.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmymathews committed Aug 21, 2023
1 parent f3371d4 commit cbdb539
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions spatialprofilingtoolbox/db/weak_lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import functools
import weakref

def weak_lru(maxsize=128, typed=False):
def wrapper(func):
@functools.lru_cache(maxsize, typed)
def _func(_self, *args, **kwargs):
return func(_self(), *args, **kwargs)
def weak_lru(*lru_args, **lru_kwargs):
def decorator(func):
@functools.wraps(func)
def inner(self, *args, **kwargs):
return _func(weakref.ref(self), *args, **kwargs)
return inner
return wrapper
def wrapped_func(self, *args, **kwargs):
self_weak = weakref.ref(self)
@functools.wraps(func)
@functools.lru_cache(*lru_args, **lru_kwargs)
def cached_method(*args, **kwargs):
return func(self_weak(), *args, **kwargs)
setattr(self, func.__name__, cached_method)
return cached_method(*args, **kwargs)
return wrapped_func
return decorator

0 comments on commit cbdb539

Please sign in to comment.