Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make itertools.batched thread-safe
  • Loading branch information
eendebakpt committed Jan 28, 2025
commit 070c8777fb4da826e85219395ad74b9f1753f614
10 changes: 5 additions & 5 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ static PyObject *
batched_next(batchedobject *bo)
{
Py_ssize_t i;
Py_ssize_t n = bo->batch_size;
Py_ssize_t n = FT_ATOMIC_LOAD_SSIZE_RELAXED(bo->batch_size);
PyObject *it = bo->it;
PyObject *item;
PyObject *result;

if (it == NULL) {
if (n < 0) {
return NULL;
}
result = PyTuple_New(n);
Expand All @@ -213,19 +213,19 @@ batched_next(batchedobject *bo)
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
/* Input raised an exception other than StopIteration */
Py_CLEAR(bo->it);
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
Py_DECREF(result);
return NULL;
}
PyErr_Clear();
}
if (i == 0) {
Py_CLEAR(bo->it);
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
Py_DECREF(result);
return NULL;
}
if (bo->strict) {
Py_CLEAR(bo->it);
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
Py_DECREF(result);
PyErr_SetString(PyExc_ValueError, "batched(): incomplete batch");
return NULL;
Expand Down