Skip to content

Commit 50b45c4

Browse files
authored
[3.13] GH-133543: Maintain tracking for materialized instance dictionaries (GH-133617)
1 parent 05ddd06 commit 50b45c4

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_dict.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,18 @@ class MyDict(dict):
10061006
pass
10071007
self._tracked(MyDict())
10081008

1009+
@support.cpython_only
1010+
def test_track_lazy_instance_dicts(self):
1011+
class C:
1012+
pass
1013+
o = C()
1014+
d = o.__dict__
1015+
self._not_tracked(d)
1016+
o.untracked = 42
1017+
self._not_tracked(d)
1018+
o.tracked = []
1019+
self._tracked(d)
1020+
10091021
def make_shared_key_dict(self, n):
10101022
class C:
10111023
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a possible memory leak that could occur when directly accessing instance
2+
dictionaries (``__dict__``) that later become part of a reference cycle.

Objects/dictobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6839,6 +6839,9 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
68396839
value == NULL ? PyDict_EVENT_DELETED :
68406840
PyDict_EVENT_MODIFIED);
68416841
_PyDict_NotifyEvent(interp, event, dict, name, value);
6842+
if (value) {
6843+
MAINTAIN_TRACKING(dict, name, value);
6844+
}
68426845
}
68436846

68446847
FT_ATOMIC_STORE_PTR_RELEASE(values->values[ix], Py_XNewRef(value));

0 commit comments

Comments
 (0)