Skip to content

Commit 7291ac0

Browse files
committed
fix refleak issue in typing.py (#98253)
1 parent 75a6fad commit 7291ac0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Lib/typing.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,23 @@ def _flatten_literal_params(parameters):
344344

345345

346346
_cleanups = []
347+
_caches = { }
347348

348349

349350
def _tp_cache(func=None, /, *, typed=False):
350351
"""Internal wrapper caching __getitem__ of generic types with a fallback to
351352
original function for non-hashable arguments.
352353
"""
353354
def decorator(func):
354-
cached = functools.lru_cache(typed=typed)(func)
355-
_cleanups.append(cached.cache_clear)
355+
cache = functools.lru_cache(typed=typed)(func)
356+
_caches[func] = cache
357+
_cleanups.append(cache.cache_clear)
358+
del cache
356359

357360
@functools.wraps(func)
358361
def inner(*args, **kwds):
359362
try:
360-
return cached(*args, **kwds)
363+
return _caches[func](*args, **kwds)
361364
except TypeError:
362365
pass # All real errors (not unhashable args) are raised below.
363366
return func(*args, **kwds)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a source of potential type-related refleaks caused by LRU caches in
2+
``typing``.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy