This commit is contained in:
Benjamin Peterson 2012-10-31 14:10:04 -04:00
commit 7503e08588
3 changed files with 41 additions and 29 deletions

View File

@ -256,6 +256,14 @@ class DictTest(unittest.TestCase):
d = dict(zip(range(6), range(6))) d = dict(zip(range(6), range(6)))
self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*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): def test_copy(self):
d = {1:1, 2:2, 3:3} d = {1:1, 2:2, 3:3}
self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) self.assertEqual(d.copy(), {1:1, 2:2, 3:3})

View File

@ -21,6 +21,9 @@ Core and Builtins
- Issue #14625: Rewrite the UTF-32 decoder. It is now 3x to 4x faster. Patch - Issue #14625: Rewrite the UTF-32 decoder. It is now 3x to 4x faster. Patch
written by Serhiy Storchaka. written by Serhiy Storchaka.
- 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 - Issue #16271: Fix strange bugs that resulted from __qualname__ appearing in a
class's __dict__ and on type. class's __dict__ and on type.

View File

@ -1707,45 +1707,46 @@ dict_fromkeys(PyObject *cls, PyObject *args)
if (d == NULL) if (d == NULL)
return NULL; return NULL;
if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { if (PyDict_CheckExact(d) && PyDict_Size(d) == 0) {
PyDictObject *mp = (PyDictObject *)d; if (PyDict_CheckExact(seq)) {
PyObject *oldvalue; PyDictObject *mp = (PyDictObject *)d;
Py_ssize_t pos = 0; PyObject *oldvalue;
PyObject *key; Py_ssize_t pos = 0;
Py_hash_t hash; PyObject *key;
Py_hash_t hash;
if (dictresize(mp, Py_SIZE(seq))) { 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); Py_DECREF(d);
return NULL; 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 (PyAnySet_CheckExact(seq)) {
} PyDictObject *mp = (PyDictObject *)d;
Py_ssize_t pos = 0;
PyObject *key;
Py_hash_t hash;
if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { if (dictresize(mp, PySet_GET_SIZE(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); Py_DECREF(d);
return NULL; 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); it = PyObject_GetIter(seq);