Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a5259fb0
Commit
a5259fb0
authored
Oct 20, 2018
by
Sergey Fedoseev
Committed by
Serhiy Storchaka
Oct 20, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-34574: Prevent OrderedDict iterators from exhaustion during pickling. (GH-9051)
parent
8c9fd9c9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
28 deletions
+28
-28
Lib/test/test_ordered_dict.py
Lib/test/test_ordered_dict.py
+17
-0
Misc/NEWS.d/next/Library/2018-09-04-09-32-54.bpo-34574.X4RwYI.rst
...S.d/next/Library/2018-09-04-09-32-54.bpo-34574.X4RwYI.rst
+2
-0
Objects/odictobject.c
Objects/odictobject.c
+9
-28
No files found.
Lib/test/test_ordered_dict.py
View file @
a5259fb0
...
...
@@ -732,6 +732,23 @@ class CPythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
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
):
...
...
Misc/NEWS.d/next/Library/2018-09-04-09-32-54.bpo-34574.X4RwYI.rst
0 → 100644
View file @
a5259fb0
OrderedDict iterators are not exhausted during pickling anymore. Patch by
Sergey Fedoseev.
Objects/odictobject.c
View file @
a5259fb0
...
...
@@ -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
);
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
[]
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment