mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
[3.12] gh-112087: Fix reduce logic for the empty reverse iterator for list (gh-115471)
This commit is contained in:
parent
7d9ce3c867
commit
5ec52c35eb
3 changed files with 9 additions and 8 deletions
|
@ -302,7 +302,7 @@ class TestCase(unittest.TestCase):
|
||||||
# listiter_reduce_general
|
# listiter_reduce_general
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
run("reversed", orig["reversed"](list(range(8)))),
|
run("reversed", orig["reversed"](list(range(8)))),
|
||||||
(iter, ([],))
|
(reversed, ([],))
|
||||||
)
|
)
|
||||||
|
|
||||||
for case in types:
|
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.
|
|
@ -3441,6 +3441,7 @@ static PyObject *
|
||||||
listiter_reduce_general(void *_it, int forward)
|
listiter_reduce_general(void *_it, int forward)
|
||||||
{
|
{
|
||||||
PyObject *list;
|
PyObject *list;
|
||||||
|
PyObject *iter;
|
||||||
|
|
||||||
/* _PyEval_GetBuiltin can invoke arbitrary code,
|
/* _PyEval_GetBuiltin can invoke arbitrary code,
|
||||||
* call must be before access of iterator pointers.
|
* call must be before access of iterator pointers.
|
||||||
|
@ -3448,7 +3449,7 @@ listiter_reduce_general(void *_it, int forward)
|
||||||
|
|
||||||
/* the objects are not the same, index is of different types! */
|
/* the objects are not the same, index is of different types! */
|
||||||
if (forward) {
|
if (forward) {
|
||||||
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
|
iter = _PyEval_GetBuiltin(&_Py_ID(iter));
|
||||||
if (!iter) {
|
if (!iter) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -3456,21 +3457,19 @@ listiter_reduce_general(void *_it, int forward)
|
||||||
if (it->it_seq) {
|
if (it->it_seq) {
|
||||||
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
|
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
|
||||||
}
|
}
|
||||||
Py_DECREF(iter);
|
|
||||||
} else {
|
} else {
|
||||||
PyObject *reversed = _PyEval_GetBuiltin(&_Py_ID(reversed));
|
iter = _PyEval_GetBuiltin(&_Py_ID(reversed));
|
||||||
if (!reversed) {
|
if (!iter) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
listreviterobject *it = (listreviterobject *)_it;
|
listreviterobject *it = (listreviterobject *)_it;
|
||||||
if (it->it_seq) {
|
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 */
|
/* empty iterator, create an empty list */
|
||||||
list = PyList_New(0);
|
list = PyList_New(0);
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
return 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