Skip to content

Commit

Permalink
Merge a3e5320 into 4bf87a3
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Sep 15, 2022
2 parents 4bf87a3 + a3e5320 commit 9b061a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ What's New in astroid 2.12.10?
==============================
Release date: TBA

* ``decorators.cached`` now uses a ``WeakKeyDictionary`` to allow the garbave collector
to remove entries when possible.

Refs #1780

What's New in astroid 2.12.9?
=============================
Expand Down
5 changes: 4 additions & 1 deletion astroid/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import inspect
import sys
import warnings
import weakref
from collections.abc import Callable
from typing import TypeVar

Expand All @@ -32,8 +33,10 @@
def cached(func, instance, args, kwargs):
"""Simple decorator to cache result of method calls without args."""
cache = getattr(instance, "__cache", None)
# Use a WeakKeyDictionary to allow the garbage collector to remove entries
# that only exist in the cache.
if cache is None:
instance.__cache = cache = {}
instance.__cache = cache = weakref.WeakKeyDictionary()
try:
return cache[func]
except KeyError:
Expand Down

0 comments on commit 9b061a7

Please sign in to comment.