diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-03-04 14:46:17 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-04 14:46:17 (GMT) |
| commit | 06a3bb8c9416a53018ea23414d6018764a9e2845 (patch) | |
| tree | 3b95a3faac050cd441fc3b70ea30671dabd40dee /Lib/test/test_iter.py | |
| parent | 00791f23b78cc61d732ef4a6386c9ce255113c3e (diff) | |
| download | cpython-06a3bb8c9416a53018ea23414d6018764a9e2845.zip cpython-06a3bb8c9416a53018ea23414d6018764a9e2845.tar.gz cpython-06a3bb8c9416a53018ea23414d6018764a9e2845.tar.bz2 | |
gh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (GH-101896)
(cherry picked from commit 705487c6557c3d8866622b4d32528bf7fc2e4204)
Co-authored-by: Raj <51259329+workingpayload@users.noreply.github.com>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Diffstat (limited to 'Lib/test/test_iter.py')
| -rw-r--r-- | Lib/test/test_iter.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 6ab9b7a..30aedb0 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -348,6 +348,31 @@ class TestCase(unittest.TestCase): return i 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 def test_exception_function(self): def spam(state=[0]): |
