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.
diff --git a/Objects/setobject.c b/Objects/setobject.c
index e7a5288..aa1f4ee 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -843,36 +843,14 @@
static PyObject *
setiter_reduce(setiterobject *si, PyObject *Py_UNUSED(ignored))
{
- PyObject *list;
- setiterobject tmp;
-
- list = PyList_New(0);
- if (!list)
- return NULL;
-
/* copy the iterator state */
- tmp = *si;
+ setiterobject tmp = *si;
Py_XINCREF(tmp.si_set);
/* iterate the temporary into a list */
- for(;;) {
- PyObject *element = setiter_iternext(&tmp);
- if (element) {
- if (PyList_Append(list, element)) {
- Py_DECREF(element);
- Py_DECREF(list);
- Py_XDECREF(tmp.si_set);
- return NULL;
- }
- Py_DECREF(element);
- } else
- break;
- }
+ PyObject *list = PySequence_List((PyObject*)&tmp);
Py_XDECREF(tmp.si_set);
- /* check for error */
- if (tmp.si_set != NULL) {
- /* we have an error */
- Py_DECREF(list);
+ if (list == NULL) {
return NULL;
}
return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);