[Python-checkins] cpython (merge 3.2 -> 3.3): merge 3.2 (#16345)

benjamin.peterson python-checkins at python.org
Wed Oct 31 19:14:23 CET 2012


http://hg.python.org/cpython/rev/b68da8f2ed95
changeset:   80111:b68da8f2ed95
branch:      3.3
parent:      80104:2e7da832219d
parent:      80110:dc47b093d2a4
user:        Benjamin Peterson <benjamin at python.org>
date:        Wed Oct 31 14:09:11 2012 -0400
summary:
  merge 3.2 (#16345)

files:
  Lib/test/test_dict.py |   8 +++
  Misc/NEWS             |   3 +
  Objects/dictobject.c  |  63 +++++++++++++++---------------
  3 files changed, 43 insertions(+), 31 deletions(-)


diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -256,6 +256,14 @@
         d = dict(zip(range(6), range(6)))
         self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6)))
 
+        class baddict3(dict):
+            def __new__(cls):
+                return d
+        d = {i : i for i in range(10)}
+        res = d.copy()
+        res.update(a=None, b=None, c=None)
+        self.assertEqual(baddict3.fromkeys({"a", "b", "c"}), res)
+
     def test_copy(self):
         d = {1:1, 2:2, 3:3}
         self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@
 - Fix segfaults on setting __qualname__ on builtin types and attempting to
   delete it on any type.
 
+- Issue #16345: Fix an infinite loop when ``fromkeys`` on a dict subclass
+  recieved a nonempty dict from the constructor.
+
 - Issue #16271: Fix strange bugs that resulted from __qualname__ appearing in a
   class's __dict__ and on type.
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1707,45 +1707,46 @@
     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;
-        Py_hash_t hash;
-
-        if (dictresize(mp, Py_SIZE(seq))) {
-            Py_DECREF(d);
-            return NULL;
-        }
-
-        while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
-            if (insertdict(mp, key, hash, value)) {
+    if (PyDict_CheckExact(d) && PyDict_Size(d) == 0) {
+        if (PyDict_CheckExact(seq)) {
+            PyDictObject *mp = (PyDictObject *)d;
+            PyObject *oldvalue;
+            Py_ssize_t pos = 0;
+            PyObject *key;
+            Py_hash_t hash;
+
+            if (dictresize(mp, Py_SIZE(seq))) {
                 Py_DECREF(d);
                 return NULL;
             }
+
+            while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
+                if (insertdict(mp, key, hash, value)) {
+                    Py_DECREF(d);
+                    return NULL;
+                }
+            }
+            return d;
         }
-        return d;
-    }
-
-    if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
-        PyDictObject *mp = (PyDictObject *)d;
-        Py_ssize_t pos = 0;
-        PyObject *key;
-        Py_hash_t hash;
-
-        if (dictresize(mp, PySet_GET_SIZE(seq))) {
-            Py_DECREF(d);
-            return NULL;
-        }
-
-        while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
-            if (insertdict(mp, key, hash, value)) {
+        if (PyAnySet_CheckExact(seq)) {
+            PyDictObject *mp = (PyDictObject *)d;
+            Py_ssize_t pos = 0;
+            PyObject *key;
+            Py_hash_t hash;
+
+            if (dictresize(mp, PySet_GET_SIZE(seq))) {
                 Py_DECREF(d);
                 return NULL;
             }
+
+            while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
+                if (insertdict(mp, key, hash, value)) {
+                    Py_DECREF(d);
+                    return NULL;
+                }
+            }
+            return d;
         }
-        return d;
     }
 
     it = PyObject_GetIter(seq);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list