Skip to content

Commit 127bd9b

Browse files
miss-islington1st1
andauthored
bpo-34042: Fix dict.copy() to maintain correct total refcount (GH-8119)
(cherry picked from commit 0b75228) Co-authored-by: Yury Selivanov <yury@magic.io>
1 parent 4bd5fce commit 127bd9b

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix dict.copy() to maintain correct total refcount (as reported by
2+
sys.gettotalrefcount()).

Objects/dictobject.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ clone_combined_dict(PyDictObject *orig)
656656
/* Maintain tracking. */
657657
_PyObject_GC_TRACK(new);
658658
}
659+
660+
/* Since we copied the keys table we now have an extra reference
661+
in the system. Manually call _Py_INC_REFTOTAL to signal that
662+
we have it now; calling DK_INCREF would be an error as
663+
keys->dk_refcnt is already set to 1 (after memcpy). */
664+
_Py_INC_REFTOTAL;
665+
659666
return (PyObject *)new;
660667
}
661668

0 commit comments

Comments
 (0)