]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-133543: Maintain tracking for materialized instance dictionaries (GH-133617)
authorBrandt Bucher <brandtbucher@microsoft.com>
Mon, 12 May 2025 20:00:01 +0000 (13:00 -0700)
committerGitHub <noreply@github.com>
Mon, 12 May 2025 20:00:01 +0000 (13:00 -0700)
Lib/test/test_dict.py
Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-10-48-31.gh-issue-133543.4jcszP.rst [new file with mode: 0644]
Objects/dictobject.c

index 40b0c210ab34b00a4d0466951ae1c9ed05024367..4729132c5a5f84edf9df7ed47bdb96c2d85aced2 100644 (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
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-10-48-31.gh-issue-133543.4jcszP.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-10-48-31.gh-issue-133543.4jcszP.rst
new file mode 100644 (file)
index 0000000..0460858
--- /dev/null
@@ -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.
index e89ed467bf7c2753b4dc8a5578db19898edadb1b..501f3d2d2bcaf5733b848974071b3163bcd3725a 100644 (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));