bpo-34042: Fix dict.copy() to maintain correct total refcount (GH-8119)
(cherry picked from commit 0b75228700e0077d8bf2636e74733389514b4b2f)
Co-authored-by: Yury Selivanov <yury@magic.io>
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 7a1bceb..828eb99 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -656,6 +656,13 @@
/* Maintain tracking. */
_PyObject_GC_TRACK(new);
}
+
+ /* Since we copied the keys table we now have an extra reference
+ in the system. Manually call _Py_INC_REFTOTAL to signal that
+ we have it now; calling DK_INCREF would be an error as
+ keys->dk_refcnt is already set to 1 (after memcpy). */
+ _Py_INC_REFTOTAL;
+
return (PyObject *)new;
}