Skip to content

Commit

Permalink
[3.12] gh-112087: Fix reduce logic for the empty reverse iterator for…
Browse files Browse the repository at this point in the history
… list (gh-115471)
  • Loading branch information
corona10 authored Feb 14, 2024
1 parent 7d9ce3c commit 5ec52c3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def __eq__(self, other):
# listiter_reduce_general
self.assertEqual(
run("reversed", orig["reversed"](list(range(8)))),
(iter, ([],))
(reversed, ([],))
)

for case in types:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For an empty reverse iterator for list will be reduced to :func:`reversed`.
Patch by Donghee Na.
13 changes: 6 additions & 7 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3441,36 +3441,35 @@ static PyObject *
listiter_reduce_general(void *_it, int forward)
{
PyObject *list;
PyObject *iter;

/* _PyEval_GetBuiltin can invoke arbitrary code,
* call must be before access of iterator pointers.
* see issue #101765 */

/* the objects are not the same, index is of different types! */
if (forward) {
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
iter = _PyEval_GetBuiltin(&_Py_ID(iter));
if (!iter) {
return NULL;
}
_PyListIterObject *it = (_PyListIterObject *)_it;
if (it->it_seq) {
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
}
Py_DECREF(iter);
} else {
PyObject *reversed = _PyEval_GetBuiltin(&_Py_ID(reversed));
if (!reversed) {
iter = _PyEval_GetBuiltin(&_Py_ID(reversed));
if (!iter) {
return NULL;
}
listreviterobject *it = (listreviterobject *)_it;
if (it->it_seq) {
return Py_BuildValue("N(O)n", reversed, it->it_seq, it->it_index);
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
}
Py_DECREF(reversed);
}
/* empty iterator, create an empty list */
list = PyList_New(0);
if (list == NULL)
return NULL;
return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list);
return Py_BuildValue("N(N)", iter, list);
}

0 comments on commit 5ec52c3

Please sign in to comment.