Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Clarify comments once more
  • Loading branch information
arhadthedev authored Feb 15, 2023
commit 36b043ccc642f64508bf98b2f5c269c0aaf3f75b
27 changes: 18 additions & 9 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):
Comment thread
arhadthedev marked this conversation as resolved.
Outdated
"""Exhaust an iterator without raising StopIteration."""
list(iterator)


# Main test suite

class TestCase(unittest.TestCase):
Expand Down Expand Up @@ -267,19 +273,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()) but returns
# a non-sentinel value thus claiming that the iterator can yield more.
# Test two-argument iter() with function that exhausts its
# 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():
if spam.not_emptied:
spam.not_emptied = False
list(spam.iterator) # Implicitly call ourselves
return HAS_MORE
return NO_MORE
spam.not_emptied = True
# 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)
Expand Down