[3.13] GH-133543: Maintain tracking for materialized instance dictionaries (GH-133617)

This commit is contained in:
Brandt Bucher 2025-05-12 13:00:01 -07:00 committed by GitHub
parent 05ddd06624
commit 50b45c4f45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

View file

@ -1006,6 +1006,18 @@ class DictTest(unittest.TestCase):
pass
self._tracked(MyDict())
@support.cpython_only
def test_track_lazy_instance_dicts(self):
class C:
pass
o = C()
d = o.__dict__
self._not_tracked(d)
o.untracked = 42
self._not_tracked(d)
o.tracked = []
self._tracked(d)
def make_shared_key_dict(self, n):
class C:
pass

View file

@ -0,0 +1,2 @@
Fix a possible memory leak that could occur when directly accessing instance
dictionaries (``__dict__``) that later become part of a reference cycle.

View file

@ -6839,6 +6839,9 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
value == NULL ? PyDict_EVENT_DELETED :
PyDict_EVENT_MODIFIED);
_PyDict_NotifyEvent(interp, event, dict, name, value);
if (value) {
MAINTAIN_TRACKING(dict, name, value);
}
}
FT_ATOMIC_STORE_PTR_RELEASE(values->values[ix], Py_XNewRef(value));