mirror of https://github.com/python/cpython.git
[3.10] gh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (GH-101896) (#102422)
gh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (#101896)
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
(cherry picked from commit 705487c655
)
Co-authored-by: Raj <51259329+workingpayload@users.noreply.github.com>
This commit is contained in:
parent
6c2e052ee0
commit
fe36778968
|
@ -346,6 +346,31 @@ def spam(state=[0]):
|
||||||
return i
|
return i
|
||||||
self.check_iterator(iter(spam, 20), list(range(10)), pickle=False)
|
self.check_iterator(iter(spam, 20), list(range(10)), pickle=False)
|
||||||
|
|
||||||
|
def test_iter_function_concealing_reentrant_exhaustion(self):
|
||||||
|
# gh-101892: Test two-argument iter() with a function that
|
||||||
|
# exhausts its associated iterator but forgets to either return
|
||||||
|
# a sentinel value or raise StopIteration.
|
||||||
|
HAS_MORE = 1
|
||||||
|
NO_MORE = 2
|
||||||
|
|
||||||
|
def exhaust(iterator):
|
||||||
|
"""Exhaust an iterator without raising StopIteration."""
|
||||||
|
list(iterator)
|
||||||
|
|
||||||
|
def spam():
|
||||||
|
# Touching the iterator with exhaust() below will call
|
||||||
|
# spam() once again so protect against recursion.
|
||||||
|
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
|
# Test exception propagation through function iterator
|
||||||
def test_exception_function(self):
|
def test_exception_function(self):
|
||||||
def spam(state=[0]):
|
def spam(state=[0]):
|
||||||
|
|
|
@ -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`.
|
|
@ -223,7 +223,7 @@ calliter_iternext(calliterobject *it)
|
||||||
}
|
}
|
||||||
|
|
||||||
result = _PyObject_CallNoArg(it->it_callable);
|
result = _PyObject_CallNoArg(it->it_callable);
|
||||||
if (result != NULL) {
|
if (result != NULL && it->it_sentinel != NULL){
|
||||||
int ok;
|
int ok;
|
||||||
|
|
||||||
ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
|
ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
|
||||||
|
@ -231,7 +231,6 @@ calliter_iternext(calliterobject *it)
|
||||||
return result; /* Common case, fast path */
|
return result; /* Common case, fast path */
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_DECREF(result);
|
|
||||||
if (ok > 0) {
|
if (ok > 0) {
|
||||||
Py_CLEAR(it->it_callable);
|
Py_CLEAR(it->it_callable);
|
||||||
Py_CLEAR(it->it_sentinel);
|
Py_CLEAR(it->it_sentinel);
|
||||||
|
@ -242,6 +241,7 @@ calliter_iternext(calliterobject *it)
|
||||||
Py_CLEAR(it->it_callable);
|
Py_CLEAR(it->it_callable);
|
||||||
Py_CLEAR(it->it_sentinel);
|
Py_CLEAR(it->it_sentinel);
|
||||||
}
|
}
|
||||||
|
Py_XDECREF(result);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue