Skip to content

Commit 070c877

Browse files
committed
Make itertools.batched thread-safe
1 parent 7d27561 commit 070c877

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Modules/itertoolsmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ static PyObject *
186186
batched_next(batchedobject *bo)
187187
{
188188
Py_ssize_t i;
189-
Py_ssize_t n = bo->batch_size;
189+
Py_ssize_t n = FT_ATOMIC_LOAD_SSIZE_RELAXED(bo->batch_size);
190190
PyObject *it = bo->it;
191191
PyObject *item;
192192
PyObject *result;
193193

194-
if (it == NULL) {
194+
if (n < 0) {
195195
return NULL;
196196
}
197197
result = PyTuple_New(n);
@@ -213,19 +213,19 @@ batched_next(batchedobject *bo)
213213
if (PyErr_Occurred()) {
214214
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
215215
/* Input raised an exception other than StopIteration */
216-
Py_CLEAR(bo->it);
216+
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
217217
Py_DECREF(result);
218218
return NULL;
219219
}
220220
PyErr_Clear();
221221
}
222222
if (i == 0) {
223-
Py_CLEAR(bo->it);
223+
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
224224
Py_DECREF(result);
225225
return NULL;
226226
}
227227
if (bo->strict) {
228-
Py_CLEAR(bo->it);
228+
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
229229
Py_DECREF(result);
230230
PyErr_SetString(PyExc_ValueError, "batched(): incomplete batch");
231231
return NULL;

0 commit comments

Comments
 (0)