Skip to content

Commit

Permalink
Deleted memoized functions now garbage collected
Browse files Browse the repository at this point in the history
  • Loading branch information
cevans87 committed Jan 21, 2020
1 parent c1e4d58 commit ef5ba0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion atools/_memoize_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from time import time
from threading import Lock as SyncLock
from typing import Any, Callable, Dict, Hashable, Mapping, Optional, Tuple, Type, Union
from weakref import WeakSet


Decoratee = Union[Callable, Type]
Expand Down Expand Up @@ -492,7 +493,7 @@ def bar() -> Any: ...
"""

_default_db_path = Path.home() / '.memoize'
_all_decorators = set()
_all_decorators = WeakSet()

@staticmethod
def __call__(
Expand Down
16 changes: 16 additions & 0 deletions test/test_memoize_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,19 @@ def foo():

assert foo() == PosixPath.cwd()
assert isinstance(foo(), PosixPath)


def test_memoized_function_is_deletable() -> None:
def get_foo() -> Callable[[], None]:
@memoize
def _foo() -> None:
...

return _foo

foo = get_foo()
r = ref(foo)
assert r() is not None
del foo
# FIXME there's a race condition here. Garbage collector may not have cleaned up foo yet
assert r() is None

0 comments on commit ef5ba0a

Please sign in to comment.