https://github.com/python/cpython/commit/1739efc57dc5f4e1401c59d74e7fdb3f34f21fcf
commit: 1739efc57dc5f4e1401c59d74e7fdb3f34f21fcf
branch: 3.11
author: Donghee Na <[email protected]>
committer: corona10 <[email protected]>
date: 2024-02-14T18:43:39Z
summary:
[3.11] gh-112087: Fix reduce logic for the empty reverse iterator for list
(gh-115472)
files:
A Misc/NEWS.d/next/Core and
Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst
M Lib/test/test_iter.py
M Objects/listobject.c
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index 30aedb0db3bb3d..9606d5beab71cb 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -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:
diff --git a/Misc/NEWS.d/next/Core and
Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst b/Misc/NEWS.d/next/Core
and Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst
new file mode 100644
index 00000000000000..05c135347d6a17
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and
Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst
@@ -0,0 +1,2 @@
+For an empty reverse iterator for list will be reduced to :func:`reversed`.
+Patch by Donghee Na.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 0d4e4741fdf346..395a4c3b88b82b 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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);
}
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]