bpo-34042: Fix dict.copy() to maintain correct total refcount (GH-8119)

(cherry picked from commit 0b75228700)

Co-authored-by: Yury Selivanov <yury@magic.io>
This commit is contained in:
Miss Islington (bot) 2018-07-06 09:40:17 -07:00 committed by GitHub
parent 4bd5fce27d
commit 127bd9bfd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Fix dict.copy() to maintain correct total refcount (as reported by
sys.gettotalrefcount()).

View File

@ -656,6 +656,13 @@ clone_combined_dict(PyDictObject *orig)
/* Maintain tracking. */ /* Maintain tracking. */
_PyObject_GC_TRACK(new); _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; return (PyObject *)new;
} }