]> granicus.if.org Git - python/commitdiff
bpo-34042: Fix dict.copy() to maintain correct total refcount (GH-8119)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 6 Jul 2018 16:40:17 +0000 (09:40 -0700)
committerGitHub <noreply@github.com>
Fri, 6 Jul 2018 16:40:17 +0000 (09:40 -0700)
(cherry picked from commit 0b75228700e0077d8bf2636e74733389514b4b2f)

Co-authored-by: Yury Selivanov <yury@magic.io>
Misc/NEWS.d/next/Core and Builtins/2018-07-05-15-51-29.bpo-34042.Gr9XUH.rst [new file with mode: 0644]
Objects/dictobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-05-15-51-29.bpo-34042.Gr9XUH.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-05-15-51-29.bpo-34042.Gr9XUH.rst
new file mode 100644 (file)
index 0000000..fd1730d
--- /dev/null
@@ -0,0 +1,2 @@
+Fix dict.copy() to maintain correct total refcount (as reported by
+sys.gettotalrefcount()).
index 7a1bcebec6fdfd30b4ffe108f6540e65313c64b9..828eb99e2d03220a9c0948c870d9725c2f2ba0e0 100644 (file)
@@ -656,6 +656,13 @@ clone_combined_dict(PyDictObject *orig)
         /* 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;
 }