From 0b75228700e0077d8bf2636e74733389514b4b2f Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Fri, 6 Jul 2018 12:20:07 -0400 Subject: bpo-34042: Fix dict.copy() to maintain correct total refcount (GH-8119) --- .../Core and Builtins/2018-07-05-15-51-29.bpo-34042.Gr9XUH.rst | 2 ++ Objects/dictobject.c | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-07-05-15-51-29.bpo-34042.Gr9XUH.rst 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 index 0000000..fd1730d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-07-05-15-51-29.bpo-34042.Gr9XUH.rst @@ -0,0 +1,2 @@ +Fix dict.copy() to maintain correct total refcount (as reported by +sys.gettotalrefcount()). diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 40d7d8a..413557d 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -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; } -- cgit v0.12