Skip to content

Commit

Permalink
break potential reference cycles in external code worsened by typing.…
Browse files Browse the repository at this point in the history
…py lru_cache (python#98253)
  • Loading branch information
wjakob committed Oct 31, 2022
1 parent 75a6fad commit 1ce6651
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,23 @@ def _flatten_literal_params(parameters):


_cleanups = []
_caches = { }


def _tp_cache(func=None, /, *, typed=False):
"""Internal wrapper caching __getitem__ of generic types with a fallback to
original function for non-hashable arguments.
"""
def decorator(func):
cached = functools.lru_cache(typed=typed)(func)
_cleanups.append(cached.cache_clear)
cache = functools.lru_cache(typed=typed)(func)
_caches[func] = cache
_cleanups.append(cache.cache_clear)
del cache

@functools.wraps(func)
def inner(*args, **kwds):
try:
return cached(*args, **kwds)
return _caches[func](*args, **kwds)
except TypeError:
pass # All real errors (not unhashable args) are raised below.
return func(*args, **kwds)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a source of potential type-related refleaks caused by LRU caches in
``typing``.

0 comments on commit 1ce6651

Please sign in to comment.