File tree 1 file changed +13
-13
lines changed
1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change @@ -142,35 +142,35 @@ static PyObject *
142
142
batched_next (batchedobject * bo )
143
143
{
144
144
Py_ssize_t i ;
145
+ Py_ssize_t n = bo -> batch_size ;
145
146
PyObject * it = bo -> it ;
146
147
PyObject * item ;
147
148
PyObject * result ;
148
149
149
150
if (it == NULL ) {
150
151
return NULL ;
151
152
}
152
- result = PyList_New (0 );
153
+ result = PyList_New (n );
153
154
if (result == NULL ) {
154
155
return NULL ;
155
156
}
156
- for (i = 0 ; i < bo -> batch_size ; i ++ ) {
157
+ for (i = 0 ; i < n ; i ++ ) {
157
158
item = PyIter_Next (it );
158
159
if (item == NULL ) {
159
160
break ;
160
161
}
161
- if (PyList_Append (result , item ) < 0 ) {
162
- Py_DECREF (item );
163
- Py_DECREF (result );
164
- return NULL ;
165
- }
166
- Py_DECREF (item );
162
+ PyList_SET_ITEM (result , i , item );
167
163
}
168
- if (PyList_GET_SIZE (result ) > 0 ) {
169
- return result ;
164
+ if (i == 0 ) {
165
+ Py_CLEAR (bo -> it );
166
+ Py_DECREF (result );
167
+ return NULL ;
170
168
}
171
- Py_CLEAR (bo -> it );
172
- Py_DECREF (result );
173
- return NULL ;
169
+ if (i < n ) {
170
+ PyObject * short_list = PyList_GetSlice (result , 0 , i );
171
+ Py_SETREF (result , short_list );
172
+ }
173
+ return result ;
174
174
}
175
175
176
176
static PyTypeObject batched_type = {
You can’t perform that action at this time.
0 commit comments