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 (#98253)
  • Loading branch information
wjakob committed Oct 31, 2022
1 parent 75a6fad commit 2fed985
Show file tree
Hide file tree
Showing 2 changed files with 16 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,10 @@
The implementation of the typing module is now more resilient to reference
leaks in binary extension modules.

Previously, a reference leak in a typed C API-based extension module could leak
internals of the typing module, which could in turn introduce leaks in
essentially any other package with typed function signatures. Although the
typing package is not the original source of the problem, such non-local
dependences exacerbate debugging of large-scale projects, and the
implementation was therefore changed to reduce harm by providing better
isolation.

0 comments on commit 2fed985

Please sign in to comment.