summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2018-10-20 00:43:33 (GMT)
committerPablo Galindo <Pablogsal@gmail.com>2018-10-20 00:43:33 (GMT)
commit6395844e6adebc12c4eba1fb75c5e7c9c8b89f85 (patch)
tree7e56ef9face3621c7de7a474495172dcde55f55a /Objects/dictobject.c
parent027664a3d5ebad575aafe5fcc572e3b05f7f24e5 (diff)
downloadcpython-6395844e6adebc12c4eba1fb75c5e7c9c8b89f85.zip
cpython-6395844e6adebc12c4eba1fb75c5e7c9c8b89f85.tar.gz
cpython-6395844e6adebc12c4eba1fb75c5e7c9c8b89f85.tar.bz2
bpo-34573: Simplify __reduce__() of set and dict iterators. (GH-9050)
Simplify the pickling of set and dictionary objects iterators by consuming the iterator into a list with PySequence_List.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r--Objects/dictobject.c38
1 files changed, 4 insertions, 34 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index fec6967..370895d 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -3663,44 +3663,14 @@ PyTypeObject PyDictIterItem_Type = {
static PyObject *
dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
{
- PyObject *list;
- dictiterobject tmp;
-
- list = PyList_New(0);
- if (!list)
- return NULL;
-
- /* copy the itertor state */
- tmp = *di;
+ /* copy the iterator state */
+ dictiterobject tmp = *di;
Py_XINCREF(tmp.di_dict);
/* iterate the temporary into a list */
- for(;;) {
- PyObject *element = 0;
- if (Py_TYPE(di) == &PyDictIterItem_Type)
- element = dictiter_iternextitem(&tmp);
- else if (Py_TYPE(di) == &PyDictIterKey_Type)
- element = dictiter_iternextkey(&tmp);
- else if (Py_TYPE(di) == &PyDictIterValue_Type)
- element = dictiter_iternextvalue(&tmp);
- else
- Py_UNREACHABLE();
- if (element) {
- if (PyList_Append(list, element)) {
- Py_DECREF(element);
- Py_DECREF(list);
- Py_XDECREF(tmp.di_dict);
- return NULL;
- }
- Py_DECREF(element);
- } else
- break;
- }
+ PyObject *list = PySequence_List((PyObject*)&tmp);
Py_XDECREF(tmp.di_dict);
- /* check for error */
- if (tmp.di_dict != NULL) {
- /* we have an error */
- Py_DECREF(list);
+ if (list == NULL) {
return NULL;
}
return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);