Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101892: Fix SystemError when a callable iterator call exhausts the iterator #101896

Merged
merged 19 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
26 changes: 26 additions & 0 deletions Lib/test/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def __call__(self):
raise IndexError # Emergency stop
return i


def exhaust(iterator):
arhadthedev marked this conversation as resolved.
Show resolved Hide resolved
"""Exhaust an iterator without raising StopIteration."""
list(iterator)


# Main test suite

class TestCase(unittest.TestCase):
Expand Down Expand Up @@ -267,6 +273,26 @@ def spam(state=[0]):
return i
self.check_iterator(iter(spam, 20), list(range(10)), pickle=False)

# Test two-argument iter() with function that exhausts its
arhadthedev marked this conversation as resolved.
Show resolved Hide resolved
# associated iterator but forgets to either return a sentinel value
# or raise `StopIteration`.
def test_iter_function_concealing_reentrant_exhaustion(self):
HAS_MORE = 1
NO_MORE = 2
def spam():
# Touching the iterator with exhaust() below will call
# spam() once again
if spam.is_recursive_call:
return NO_MORE
spam.is_recursive_call = True
exhaust(spam.iterator)
return HAS_MORE

spam.is_recursive_call = False
spam.iterator = iter(spam, NO_MORE)
with self.assertRaises(StopIteration):
next(spam.iterator)

# Test exception propagation through function iterator
def test_exception_function(self):
def spam(state=[0]):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Callable iterators no longer raise :class:`SystemError` when the
callable object exhausts the iterator but forgets to either return a
sentinel value or raise :class:`StopIteration`.
4 changes: 2 additions & 2 deletions Objects/iterobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,14 @@ calliter_iternext(calliterobject *it)
}

result = _PyObject_CallNoArgs(it->it_callable);
if (result != NULL) {
if (result != NULL && it->it_sentinel != NULL){
int ok;

ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
if (ok == 0) {
return result; /* Common case, fast path */
}

Py_DECREF(result);
if (ok > 0) {
Py_CLEAR(it->it_callable);
Py_CLEAR(it->it_sentinel);
Expand All @@ -233,6 +232,7 @@ calliter_iternext(calliterobject *it)
Py_CLEAR(it->it_callable);
Py_CLEAR(it->it_sentinel);
}
Py_XDECREF(result);
return NULL;
}

Expand Down