summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorOleg Iarygin <oleg@arhadthedev.net>2023-03-04 15:26:12 (GMT)
committerGitHub <noreply@github.com>2023-03-04 15:26:12 (GMT)
commitfe36778968a0fdddada282fbd7e28e8ccd0debe0 (patch)
treed48878c8dbb6b7848afdee365cadd8a1b01c2a34 /Lib/test
parent6c2e052ee07f10a6336bb4de1cef71dbe7d30ee6 (diff)
downloadcpython-fe36778968a0fdddada282fbd7e28e8ccd0debe0.zip
cpython-fe36778968a0fdddada282fbd7e28e8ccd0debe0.tar.gz
cpython-fe36778968a0fdddada282fbd7e28e8ccd0debe0.tar.bz2
[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 705487c6557c3d8866622b4d32528bf7fc2e4204) Co-authored-by: Raj <51259329+workingpayload@users.noreply.github.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_iter.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index 3130ec5..f9935d6 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -346,6 +346,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]):