Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 9b99bbf..00f9bc8 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1184,6 +1184,25 @@
if (d == NULL)
return NULL;
+ if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
+ PyDictObject *mp = (PyDictObject *)d;
+ PyObject *oldvalue;
+ Py_ssize_t pos = 0;
+ PyObject *key;
+ long hash;
+
+ if (dictresize(mp, ((PyDictObject *)seq)->ma_used))
+ return NULL;
+
+ while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
+ Py_INCREF(key);
+ Py_INCREF(value);
+ if (insertdict(mp, key, hash, value))
+ return NULL;
+ }
+ return d;
+ }
+
if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
PyDictObject *mp = (PyDictObject *)d;
Py_ssize_t pos = 0;