mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
[3.11] gh-112087: Fix reduce logic for the empty reverse iterator for list (gh-115472)
This commit is contained in:
parent
c12b0a25e8
commit
1739efc57d
3 changed files with 9 additions and 8 deletions
|
@ -302,7 +302,7 @@ class TestCase(unittest.TestCase):
|
|||
# listiter_reduce_general
|
||||
self.assertEqual(
|
||||
run("reversed", orig["reversed"](list(range(8)))),
|
||||
(iter, ([],))
|
||||
(reversed, ([],))
|
||||
)
|
||||
|
||||
for case in types:
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
For an empty reverse iterator for list will be reduced to :func:`reversed`.
|
||||
Patch by Donghee Na.
|
|
@ -3451,6 +3451,7 @@ 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.
|
||||
|
@ -3458,7 +3459,7 @@ listiter_reduce_general(void *_it, int forward)
|
|||
|
||||
/* 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;
|
||||
}
|
||||
|
@ -3466,21 +3467,19 @@ listiter_reduce_general(void *_it, int forward)
|
|||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue