diff --git a/Misc/NEWS b/Misc/NEWS index eba6979507e..e203a2d56ff 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,9 @@ Core and Builtins Library ------- +- Issue #29019: Fix dict.fromkeys(x) overallocates when x is sparce dict. + Original patch by Rasmus Villemoes. + - Issue #19542: Fix bugs in WeakValueDictionary.setdefault() and WeakValueDictionary.pop() when a GC collection happens in another thread. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index e3e4765d0a1..ebd352d8e04 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1391,7 +1391,7 @@ dict_fromkeys(PyObject *cls, PyObject *args) PyObject *key; long hash; - if (dictresize(mp, Py_SIZE(seq) / 2 * 3)) { + if (dictresize(mp, ((PyDictObject *)seq)->ma_used / 2 * 3)) { Py_DECREF(d); return NULL; }