Skip to content

Commit 11491c1

Browse files
committed
Make itertools.dropwhile and itertools.takewhile safe in the free-threading build
1 parent 36b5284 commit 11491c1

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make concurrent iteration over :class:`itertools.dropwhile` and
2+
:class:`itertools.takewhile` safe under free-threading.

Modules/itertoolsmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ dropwhile_next(PyObject *op)
13401340
item = iternext(it);
13411341
if (item == NULL)
13421342
return NULL;
1343-
if (lz->start == 1)
1343+
if (FT_ATOMIC_LOAD_LONG_RELAXED(lz->start) == 1)
13441344
return item;
13451345

13461346
good = PyObject_CallOneArg(lz->func, item);
@@ -1351,7 +1351,7 @@ dropwhile_next(PyObject *op)
13511351
ok = PyObject_IsTrue(good);
13521352
Py_DECREF(good);
13531353
if (ok == 0) {
1354-
lz->start = 1;
1354+
FT_ATOMIC_STORE_LONG_RELAXED(lz->start, 1);
13551355
return item;
13561356
}
13571357
Py_DECREF(item);
@@ -1457,7 +1457,7 @@ takewhile_next(PyObject *op)
14571457
PyObject *it = lz->it;
14581458
long ok;
14591459

1460-
if (lz->stop == 1)
1460+
if (FT_ATOMIC_LOAD_LONG_RELAXED(lz->stop) == 1)
14611461
return NULL;
14621462

14631463
item = (*Py_TYPE(it)->tp_iternext)(it);
@@ -1475,7 +1475,7 @@ takewhile_next(PyObject *op)
14751475
return item;
14761476
Py_DECREF(item);
14771477
if (ok == 0)
1478-
lz->stop = 1;
1478+
FT_ATOMIC_STORE_LONG_RELAXED(lz->stop, 1);
14791479
return NULL;
14801480
}
14811481

0 commit comments

Comments
 (0)