gh-118013: Use weakrefs for the cache key in inspect._shadowed_dict (#118202)

This commit is contained in:
Alex Waygood 2024-04-24 15:55:02 +01:00 committed by GitHub
parent 83235f7791
commit 8227883d1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 8 deletions

View file

@ -160,6 +160,7 @@ import builtins
from keyword import iskeyword
from operator import attrgetter
from collections import namedtuple, OrderedDict
from weakref import ref as make_weakref
# Create constants for the compiler flags in Include/code.h
# We try to get them from dis to avoid duplication
@ -1832,9 +1833,16 @@ def _check_class(klass, attr):
return entry.__dict__[attr]
return _sentinel
@functools.lru_cache()
def _shadowed_dict_from_mro_tuple(mro):
for entry in mro:
def _shadowed_dict_from_weakref_mro_tuple(*weakref_mro):
for weakref_entry in weakref_mro:
# Normally we'd have to check whether the result of weakref_entry()
# is None here, in case the object the weakref is pointing to has died.
# In this specific case, however, we know that the only caller of this
# function is `_shadowed_dict()`, and that therefore this weakref is
# guaranteed to point to an object that is still alive.
entry = weakref_entry()
dunder_dict = _get_dunder_dict_of_class(entry)
if '__dict__' in dunder_dict:
class_dict = dunder_dict['__dict__']
@ -1844,8 +1852,19 @@ def _shadowed_dict_from_mro_tuple(mro):
return class_dict
return _sentinel
def _shadowed_dict(klass):
return _shadowed_dict_from_mro_tuple(_static_getmro(klass))
# gh-118013: the inner function here is decorated with lru_cache for
# performance reasons, *but* make sure not to pass strong references
# to the items in the mro. Doing so can lead to unexpected memory
# consumption in cases where classes are dynamically created and
# destroyed, and the dynamically created classes happen to be the only
# objects that hold strong references to other objects that take up a
# significant amount of memory.
return _shadowed_dict_from_weakref_mro_tuple(
*[make_weakref(entry) for entry in _static_getmro(klass)]
)
def getattr_static(obj, attr, default=_sentinel):
"""Retrieve attributes without triggering dynamic lookup via the