[Python-checkins] r58895 - in python/trunk: Lib/test/test_dict.py Objects/dictobject.c
raymond.hettinger
python-checkins at python.org
Wed Nov 7 03:26:17 CET 2007
Author: raymond.hettinger
Date: Wed Nov 7 03:26:17 2007
New Revision: 58895
Modified:
python/trunk/Lib/test/test_dict.py
python/trunk/Objects/dictobject.c
Log:
Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Modified: python/trunk/Lib/test/test_dict.py
==============================================================================
--- python/trunk/Lib/test/test_dict.py (original)
+++ python/trunk/Lib/test/test_dict.py Wed Nov 7 03:26:17 2007
@@ -243,6 +243,10 @@
self.assertRaises(Exc, baddict2.fromkeys, [1])
+ # test fast path for dictionary inputs
+ d = dict(zip(range(6), range(6)))
+ self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6)))
+
def test_copy(self):
d = {1:1, 2:2, 3:3}
self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
Modified: python/trunk/Objects/dictobject.c
==============================================================================
--- python/trunk/Objects/dictobject.c (original)
+++ python/trunk/Objects/dictobject.c Wed Nov 7 03:26:17 2007
@@ -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;
More information about the Python-checkins
mailing list