Skip to content

Commit

Permalink
Make reversed iterator thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
eendebakpt committed Jun 24, 2024
1 parent 56657f6 commit 57bc2dc
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,20 +429,21 @@ static PyObject *
reversed_next(reversedobject *ro)
{
PyObject *item;
Py_ssize_t index = ro->index;
Py_ssize_t index = _Py_atomic_add_ssize(&(ro->index), -1);

if (index >= 0) {
item = PySequence_GetItem(ro->seq, index);
if (item != NULL) {
ro->index--;
return item;
}
if (PyErr_ExceptionMatches(PyExc_IndexError) ||
PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
}
ro->index = -1;
FT_ATOMIC_STORE_SSIZE_RELAXED(ro->index, -1);
#ifndef Py_GIL_DISABLED
Py_CLEAR(ro->seq);
#endif
return NULL;
}

Expand Down

0 comments on commit 57bc2dc

Please sign in to comment.