gh-123471: Make concurrent iteration over itertools.repeat safe under free-threading (#131247)

This commit is contained in:
Pieter Eendebak 2025-04-13 09:56:58 +02:00 committed by GitHub
parent 5863cd70b8
commit 9d127e83b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View file

@ -0,0 +1 @@
Make concurrent iterations over :class:`itertools.repeat` safe under free-threading.

View file

@ -3628,10 +3628,14 @@ static PyObject *
repeat_next(PyObject *op) repeat_next(PyObject *op)
{ {
repeatobject *ro = repeatobject_CAST(op); repeatobject *ro = repeatobject_CAST(op);
if (ro->cnt == 0) Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->cnt);
if (cnt == 0) {
return NULL; return NULL;
if (ro->cnt > 0) }
ro->cnt--; if (cnt > 0) {
cnt--;
FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt);
}
return Py_NewRef(ro->element); return Py_NewRef(ro->element);
} }