From 7c890ed19c7bec10fc754199cf59767822e7508a Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 22 Oct 2022 01:19:36 -0500 Subject: [PATCH 1/2] GH-98363: Slicing isn't necessary. A resize will suffice. --- Modules/itertoolsmodule.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 578c2d94288548..e186fda00589b9 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -181,9 +181,9 @@ batched_next(batchedobject *bo) Py_DECREF(result); return NULL; } - PyObject *short_list = PyList_GetSlice(result, 0, i); - Py_DECREF(result); - return short_list; + /* Elements in result[i:] are still NULL */ + Py_SET_SIZE(result, i); + return result; } static PyTypeObject batched_type = { From 50ec7421662029e56b99aeb04d8245d3ad443ac8 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 22 Oct 2022 01:33:07 -0500 Subject: [PATCH 2/2] Minor beautification --- Modules/itertoolsmodule.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index e186fda00589b9..381ec3b31d525e 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -167,14 +167,13 @@ batched_next(batchedobject *bo) null_item: if (PyErr_Occurred()) { - if (PyErr_ExceptionMatches(PyExc_StopIteration)) { - PyErr_Clear(); - } else { - /* input raised an exception other than StopIteration */ + if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { + /* Input raised an exception other than StopIteration */ Py_CLEAR(bo->it); Py_DECREF(result); return NULL; } + PyErr_Clear(); } if (i == 0) { Py_CLEAR(bo->it);