8000 bpo-34574: Prevent OrderedDict iterators from exhaustion during pickling. by sir-sigurd · Pull Request #9051 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-34574: Prevent OrderedDict iterators from exhaustion during pickling. #9051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,23 @@ def test_key_change_during_iteration(self):
del od['c']
self.assertEqual(list(od), list('bdeaf'))

def test_iterators_pickling(self):
OrderedDict = self.OrderedDict
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
od = OrderedDict(pairs)

for method_name in ('keys', 'values', 'items'):
meth = getattr(od, method_name)
expected = list(meth())[1:]
for i in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(method_name=method_name, protocol=i):
it = iter(meth())
next(it)
p = pickle.dumps(it, i)
unpickled = pickle.loads(p)
self.assertEqual(list(unpickled), expected)
self.assertEqual(list(it), expected)


class PurePythonOrderedDictSubclassTests(PurePythonOrderedDictTests):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OrderedDict iterators are not exhausted during pickling anymore. Patch by
Sergey Fedoseev.
37 changes: 9 additions & 28 deletions Objects/odictobject.c
< 7E49 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -1837,38 +1837,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not enough. The state includes a reference to the result tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the problem wasn't caused by the result tuple.

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);
return NULL;
}
iter = _PyObject_GetBuiltin("iter");
if (iter == NULL) {
Py_DECREF(list);
PyObject *list = PySequence_List((PyObject*)&tmp);
Py_XDECREF(tmp.di_odict);
Py_XDECREF(tmp.di_current);
if (list == NULL) {
return NULL;
}
return Py_BuildValue("N(N)", iter, list);
return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
}

static PyMethodDef odictiter_methods[] = {
Expand Down
0