diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index cb90d55941cae7..19367dfcc1ccbb 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 c57dba4a5bf39e..28fb43dce4c6cb 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; }