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 6 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
16 changes: 16 additions & 0 deletions Lib/test/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,22 @@ def spam(state=[0]):
return i
self.check_iterator(iter(spam, 20), list(range(10)), pickle=False)

# Test two-argument iter() with function that empties its associated
# iterator via list() (or anything else but next()) then returns
# a non-sentinel value.
def test_iter_function_concealing_reentrant_exhaustion(self):
HAS_MORE = 1
NO_MORE = 2
def spam():
if not spam.is_reentrant:
spam.is_reentrant = True
list(spam.iterator)
return HAS_MORE
return NO_MORE
spam.is_reentrant = False
spam.iterator = iter(spam, NO_MORE)
next(spam.iterator)
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the expected behaviour here? StopIteration?

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for noticing the oversight! Fixed.


# 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 @@
Callable iterators no longer raise :class:`SystemError` by exhausting themselves.
2 changes: 1 addition & 1 deletion Objects/iterobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ calliter_iternext(calliterobject *it)
}

result = _PyObject_CallNoArgs(it->it_callable);
if (result != NULL) {
if (result != NULL && it->it_callable != NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, but this won't fix it. Notice the first if statement in the function. If it->it_callable == NULL is true, NULL is returned. So it->it_callable != NULL is already guaranteed at this point.

Copy link
Contributor Author

@workingpayload workingpayload Feb 15, 2023

Choose a reason for hiding this comment

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

What if we check it->it_sentinel != NULL ?

int ok;

ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
Expand Down