Skip to content

Commit 5871e19

Browse files
authored
GH-98363: Slicing isn't necessary. A size reduction will suffice. (GH-98538)
1 parent f7f55a5 commit 5871e19

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Modules/itertoolsmodule.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,22 @@ batched_next(batchedobject *bo)
167167

168168
null_item:
169169
if (PyErr_Occurred()) {
170-
if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
171-
PyErr_Clear();
172-
} else {
173-
/* input raised an exception other than StopIteration */
170+
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
171+
/* Input raised an exception other than StopIteration */
174172
Py_CLEAR(bo->it);
175173
Py_DECREF(result);
176174
return NULL;
177175
}
176+
PyErr_Clear();
178177
}
179178
if (i == 0) {
180179
Py_CLEAR(bo->it);
181180
Py_DECREF(result);
182181
return NULL;
183182
}
184-
PyObject *short_list = PyList_GetSlice(result, 0, i);
185-
Py_DECREF(result);
186-
return short_list;
183+
/* Elements in result[i:] are still NULL */
184+
Py_SET_SIZE(result, i);
185+
return result;
187186
}
188187

189188
static PyTypeObject batched_type = {

0 commit comments

Comments
 (0)