Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,25 @@ def __eq__(self, other):
for j in range(2):
next(g, None) # shouldn't crash

def test_zip_longest_reentrancy(self):
class Reenter:
def __iter__(self):
return self

def __next__(self):
z = self.zip_longest
if z is None:
raise StopIteration
self.zip_longest = None
next(z, None)
raise StopIteration

driver = Reenter()
driver.zip_longest = itertools.zip_longest(itertools.chain(driver), itertools.repeat(None))

with self.assertRaises(ValueError):
next(driver.zip_longest)


class SubclassWithKwargsTest(unittest.TestCase):
def test_keywords_in_subclass(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Now :func:`itertools.zip_longest` raises a :exc:`ValueError` when a reentrancy is detected.
33 changes: 26 additions & 7 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3785,6 +3785,7 @@ typedef struct {
PyObject *ittuple; /* tuple of iterators */
PyObject *result;
PyObject *fillvalue;
uint8_t running;
} ziplongestobject;

#define ziplongestobject_CAST(op) ((ziplongestobject *)(op))
Expand Down Expand Up @@ -3854,6 +3855,7 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
lz->numactive = tuplesize;
lz->result = result;
lz->fillvalue = Py_NewRef(fillvalue);
lz->running = 0;
return (PyObject *)lz;
}

Expand Down Expand Up @@ -3892,10 +3894,21 @@ zip_longest_next_lock_held(PyObject *op)
PyObject *item;
PyObject *olditem;

if (tuplesize == 0)
return NULL;
if (lz->numactive == 0)
if (lz->running == 1) {
PyErr_SetString(PyExc_ValueError,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

itertools.tee raises a RuntimeError, but generators raise a ValueError on re-entrant calls. In #150589 I picked RuntimeError for pairwise re-entrant calls.

I have no strong opinion on the type of error, but lets make it an informed decision and then be consistent. @rhettinger

"zip_longest() iterator already executing");
return NULL;
}
lz->running = 1;

if (tuplesize == 0) {
result = NULL;
goto done;
}
if (lz->numactive == 0) {
result = NULL;
goto done;
}
if (_PyObject_IsUniquelyReferenced(result)) {
Py_INCREF(result);
for (i=0 ; i < tuplesize ; i++) {
Expand All @@ -3909,7 +3922,8 @@ zip_longest_next_lock_held(PyObject *op)
if (lz->numactive == 0 || PyErr_Occurred()) {
lz->numactive = 0;
Py_DECREF(result);
return NULL;
result = NULL;
goto done;
} else {
item = Py_NewRef(lz->fillvalue);
PyTuple_SET_ITEM(lz->ittuple, i, NULL);
Expand All @@ -3926,8 +3940,10 @@ zip_longest_next_lock_held(PyObject *op)
_PyTuple_Recycle(result);
} else {
result = PyTuple_New(tuplesize);
if (result == NULL)
return NULL;
if (result == NULL) {
result = NULL;
goto done;
}
for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i);
if (it == NULL) {
Expand All @@ -3939,7 +3955,8 @@ zip_longest_next_lock_held(PyObject *op)
if (lz->numactive == 0 || PyErr_Occurred()) {
lz->numactive = 0;
Py_DECREF(result);
return NULL;
result = NULL;
goto done;
} else {
item = Py_NewRef(lz->fillvalue);
PyTuple_SET_ITEM(lz->ittuple, i, NULL);
Expand All @@ -3950,6 +3967,8 @@ zip_longest_next_lock_held(PyObject *op)
PyTuple_SET_ITEM(result, i, item);
}
}
done:
lz->running = 0;
return result;
}

Expand Down
Loading