Skip to content
Closed
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
eendebakpt committed Oct 14, 2024
commit c7ffc4f7b3c064ded698a5852445be289c75ecaa
42 changes: 42 additions & 0 deletions Lib/test/test_free_threading/test_itertools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from threading import Thread

from test.support import threading_helper

from itertools import zip_longest

class PairwiseThreading(unittest.TestCase):
@staticmethod
def work(enum):
while True:
try:
next(enum)
except StopIteration:
break

@threading_helper.reap_threads
@threading_helper.requires_working_threading()
def test_zip_longest(self):
number_of_threads = 8
number_of_iterations = 40
n = 200
enum = zip_longest(range(n), range(2*n))
for _ in range(number_of_iterations):
worker_threads = []
for ii in range(number_of_threads):
worker_threads.append(
Thread(
target=self.work,
args=[
enum,
],
)
)
for t in worker_threads:
t.start()
for t in worker_threads:
t.join()


if __name__ == "__main__":
unittest.main()
17 changes: 15 additions & 2 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ pairwise_traverse(pairwiseobject *po, visitproc visit, void *arg)
static PyObject *
pairwise_next(pairwiseobject *po)
{
#ifdef Py_GIL_DISABLED
PyObject *it = Py_XNewRef(po->it);
#else
PyObject *it = po->it;
#endif
if (it == NULL) {
return NULL;
}
Expand All @@ -339,16 +343,21 @@ pairwise_next(pairwiseobject *po)
old = (*Py_TYPE(it)->tp_iternext)(it);
if (old == NULL) {
Py_CLEAR(po->it);
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
return NULL;
}
Py_XSETREF(po->old, Py_NewRef(old));
if (po->it == NULL) {
// for re-entrant calls to pairwise next. the actual behavior is not important and this does not avoid any bugs (or does it?)
// the reason for having it is to make the behaviour equal to the python implementation behavior
// gh-109786: special case for re-entrant calls to pairwise next. the actual behavior is not
// important and this does not avoid any bugs (or does it?)
// the reason for having it is to make the behaviour equal to the python implementation
Py_CLEAR(po->old);
Py_DECREF(old);
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
return NULL;
}
}
Expand All @@ -357,7 +366,9 @@ pairwise_next(pairwiseobject *po)
if (new == NULL) {
Py_CLEAR(po->it);
Py_CLEAR(po->old);
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
Py_DECREF(old);
return NULL;
}
Expand Down Expand Up @@ -387,7 +398,9 @@ pairwise_next(pairwiseobject *po)

Py_XSETREF(po->old, new);
Py_DECREF(old); // instead of the decref here we could borrow the reference above
#ifdef Py_GIL_DISABLED
Py_DECREF(it);
#endif
return result;
}

Expand Down