[Python-checkins] Fix memory leaks in test_capi (#98017)

JelleZijlstra webhook-mailer at python.org
Fri Oct 7 11:18:10 EDT 2022


https://github.com/python/cpython/commit/be4099e55d30a2991b46add59ee96c531904c144
commit: be4099e55d30a2991b46add59ee96c531904c144
branch: main
author: Carl Meyer <carl at oddbird.net>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-10-07T08:17:41-07:00
summary:

Fix memory leaks in test_capi (#98017)

files:
M Lib/test/test_capi.py
M Modules/_testcapimodule.c

diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index cb90d55941ca..19367dfcc1cc 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -1495,6 +1495,9 @@ def unraisable_hook(unraisable):
         unraisable = unraisables[0]
         self.assertIs(unraisable.object, d)
         self.assertEqual(str(unraisable.exc_value), "boom!")
+        # avoid leaking reference cycles
+        del unraisable
+        del unraisables
 
     def test_two_watchers(self):
         d1 = {}
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index c57dba4a5bf3..28fb43dce4c6 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -5210,6 +5210,7 @@ dict_watch_callback(PyDict_WatchEvent event,
         Py_DECREF(msg);
         return -1;
     }
+    Py_DECREF(msg);
     return 0;
 }
 
@@ -5224,8 +5225,10 @@ dict_watch_callback_second(PyDict_WatchEvent event,
         return -1;
     }
     if (PyList_Append(g_dict_watch_events, msg) < 0) {
+        Py_DECREF(msg);
         return -1;
     }
+    Py_DECREF(msg);
     return 0;
 }
 



More information about the Python-checkins mailing list