Skip to content

Commit 8227883

Browse files
authored
gh-118013: Use weakrefs for the cache key in inspect._shadowed_dict (#118202)
1 parent 83235f7 commit 8227883

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

Doc/whatsnew/3.12.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,7 @@ inspect
734734

735735
* The performance of :func:`inspect.getattr_static` has been considerably
736736
improved. Most calls to the function should be at least 2x faster than they
737-
were in Python 3.11, and some may be 6x faster or more. (Contributed by Alex
738-
Waygood in :gh:`103193`.)
737+
were in Python 3.11. (Contributed by Alex Waygood in :gh:`103193`.)
739738

740739
itertools
741740
---------
@@ -1006,8 +1005,8 @@ typing
10061005
:func:`runtime-checkable protocols <typing.runtime_checkable>` has changed
10071006
significantly. Most ``isinstance()`` checks against protocols with only a few
10081007
members should be at least 2x faster than in 3.11, and some may be 20x
1009-
faster or more. However, ``isinstance()`` checks against protocols with fourteen
1010-
or more members may be slower than in Python 3.11. (Contributed by Alex
1008+
faster or more. However, ``isinstance()`` checks against protocols with many
1009+
members may be slower than in Python 3.11. (Contributed by Alex
10111010
Waygood in :gh:`74690` and :gh:`103193`.)
10121011

10131012
* All :data:`typing.TypedDict` and :data:`typing.NamedTuple` classes now have the

Lib/inspect.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
from keyword import iskeyword
161161
from operator import attrgetter
162162
from collections import namedtuple, OrderedDict
163+
from weakref import ref as make_weakref
163164

164165
# Create constants for the compiler flags in Include/code.h
165166
# We try to get them from dis to avoid duplication
@@ -1832,9 +1833,16 @@ def _check_class(klass, attr):
18321833
return entry.__dict__[attr]
18331834
return _sentinel
18341835

1836+
18351837
@functools.lru_cache()
1836-
def _shadowed_dict_from_mro_tuple(mro):
1837-
for entry in mro:
1838+
def _shadowed_dict_from_weakref_mro_tuple(*weakref_mro):
1839+
for weakref_entry in weakref_mro:
1840+
# Normally we'd have to check whether the result of weakref_entry()
1841+
# is None here, in case the object the weakref is pointing to has died.
1842+
# In this specific case, however, we know that the only caller of this
1843+
# function is `_shadowed_dict()`, and that therefore this weakref is
1844+
# guaranteed to point to an object that is still alive.
1845+
entry = weakref_entry()
18381846
dunder_dict = _get_dunder_dict_of_class(entry)
18391847
if '__dict__' in dunder_dict:
18401848
class_dict = dunder_dict['__dict__']
@@ -1844,8 +1852,19 @@ def _shadowed_dict_from_mro_tuple(mro):
18441852
return class_dict
18451853
return _sentinel
18461854

1855+
18471856
def _shadowed_dict(klass):
1848-
return _shadowed_dict_from_mro_tuple(_static_getmro(klass))
1857+
# gh-118013: the inner function here is decorated with lru_cache for
1858+
# performance reasons, *but* make sure not to pass strong references
1859+
# to the items in the mro. Doing so can lead to unexpected memory
1860+
# consumption in cases where classes are dynamically created and
1861+
# destroyed, and the dynamically created classes happen to be the only
1862+
# objects that hold strong references to other objects that take up a
1863+
# significant amount of memory.
1864+
return _shadowed_dict_from_weakref_mro_tuple(
1865+
*[make_weakref(entry) for entry in _static_getmro(klass)]
1866+
)
1867+
18491868

18501869
def getattr_static(obj, attr, default=_sentinel):
18511870
"""Retrieve attributes without triggering dynamic lookup via the

Lib/test/libregrtest/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def clear_caches():
275275
except KeyError:
276276
pass
277277
else:
278-
inspect._shadowed_dict_from_mro_tuple.cache_clear()
278+
inspect._shadowed_dict_from_weakref_mro_tuple.cache_clear()
279279
inspect._filesbymodname.clear()
280280
inspect.modulesbyfile.clear()
281281

Lib/test/test_inspect/test_inspect.py

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import copy
55
import datetime
66
import functools
7+
import gc
78
import importlib
89
import inspect
910
import io
@@ -25,6 +26,7 @@
2526
import unittest
2627
import unittest.mock
2728
import warnings
29+
import weakref
2830

2931

3032
try:
@@ -2302,6 +2304,13 @@ def __dict__(self):
23022304
self.assertEqual(inspect.getattr_static(foo, 'a'), 3)
23032305
self.assertFalse(test.called)
23042306

2307+
class Bar(Foo): pass
2308+
2309+
bar = Bar()
2310+
bar.a = 5
2311+
self.assertEqual(inspect.getattr_static(bar, 'a'), 3)
2312+
self.assertFalse(test.called)
2313+
23052314
def test_mutated_mro(self):
23062315
test = self
23072316
test.called = False
@@ -2406,6 +2415,21 @@ def __getattribute__(self, attr):
24062415

24072416
self.assertFalse(test.called)
24082417

2418+
def test_cache_does_not_cause_classes_to_persist(self):
2419+
# regression test for gh-118013:
2420+
# check that the internal _shadowed_dict cache does not cause
2421+
# dynamically created classes to have extended lifetimes even
2422+
# when no other strong references to those classes remain.
2423+
# Since these classes can themselves hold strong references to
2424+
# other objects, this can cause unexpected memory consumption.
2425+
class Foo: pass
2426+
Foo.instance = Foo()
2427+
weakref_to_class = weakref.ref(Foo)
2428+
inspect.getattr_static(Foo.instance, 'whatever', 'irrelevant')
2429+
del Foo
2430+
gc.collect()
2431+
self.assertIsNone(weakref_to_class())
2432+
24092433

24102434
class TestGetGeneratorState(unittest.TestCase):
24112435

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Fix regression introduced in gh-103193 that meant that calling
2+
:func:`inspect.getattr_static` on an instance would cause a strong reference
3+
to that instance's class to persist in an internal cache in the
4+
:mod:`inspect` module. This caused unexpected memory consumption if the
5+
class was dynamically created, the class held strong references to other
6+
objects which took up a significant amount of memory, and the cache
7+
contained the sole strong reference to the class. The fix for the regression
8+
leads to a slowdown in :func:`getattr_static`, but the function should still
9+
be signficantly faster than it was in Python 3.11. Patch by Alex Waygood.

0 commit comments

Comments
 (0)