mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
bpo-34574: Prevent OrderedDict iterators from exhaustion during pickling. (GH-9051)
This commit is contained in:
parent
8c9fd9c91b
commit
a5259fb05d
3 changed files with 28 additions and 28 deletions
|
|
@ -1805,38 +1805,19 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
|
|||
static PyObject *
|
||||
odictiter_reduce(odictiterobject *di)
|
||||
{
|
||||
PyObject *list, *iter;
|
||||
|
||||
list = PyList_New(0);
|
||||
if (!list)
|
||||
return NULL;
|
||||
/* copy the iterator state */
|
||||
odictiterobject tmp = *di;
|
||||
Py_XINCREF(tmp.di_odict);
|
||||
Py_XINCREF(tmp.di_current);
|
||||
|
||||
/* iterate the temporary into a list */
|
||||
for(;;) {
|
||||
PyObject *element = odictiter_iternext(di);
|
||||
if (element) {
|
||||
if (PyList_Append(list, element)) {
|
||||
Py_DECREF(element);
|
||||
Py_DECREF(list);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(element);
|
||||
}
|
||||
else {
|
||||
/* done iterating? */
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
Py_DECREF(list);
|
||||
PyObject *list = PySequence_List((PyObject*)&tmp);
|
||||
Py_XDECREF(tmp.di_odict);
|
||||
Py_XDECREF(tmp.di_current);
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
iter = _PyObject_GetBuiltin("iter");
|
||||
if (iter == NULL) {
|
||||
Py_DECREF(list);
|
||||
return NULL;
|
||||
}
|
||||
return Py_BuildValue("N(N)", iter, list);
|
||||
return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
|
||||
}
|
||||
|
||||
static PyMethodDef odictiter_methods[] = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue