Skip to content

gh-107208: Fix iter_index() recipe to not swallow exceptions #108835

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

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Changes from all 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
35 changes: 23 additions & 12 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -865,26 +865,22 @@ which incur interpreter overhead.
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
return next(filter(pred, iterable), default)

def iter_index(iterable, value, start=0):
def iter_index(iterable, value, start=0, stop=None):
"Return indices where a value occurs in a sequence or iterable."
# iter_index('AABCADEAF', 'A') --> 0 1 4 7
try:
seq_index = iterable.index
except AttributeError:
seq_index = getattr(iterable, 'index', None)
if seq_index is None:
# Slow path for general iterables
it = islice(iterable, start, None)
i = start - 1
try:
while True:
yield (i := i + operator.indexOf(it, value) + 1)
except ValueError:
pass
it = islice(iterable, start, stop)
for i, element in enumerate(it, start):
if element is value or element == value:
yield i
else:
# Fast path for sequences
i = start - 1
try:
while True:
yield (i := seq_index(value, i+1))
yield (i := seq_index(value, i+1, stop))
except ValueError:
pass

Expand Down Expand Up @@ -1331,6 +1327,21 @@ The following recipes have a more mathematical flavor:
[]
>>> list(iter_index(iter('AABCADEAF'), 'A', 10))
[]
>>> list(iter_index('AABCADEAF', 'A', 1, 7))
[1, 4]
>>> list(iter_index(iter('AABCADEAF'), 'A', 1, 7))
[1, 4]
>>> # Verify that ValueErrors not swallowed (gh-107208)
>>> def assert_no_value(iterable, forbidden_value):
... for item in iterable:
... if item == forbidden_value:
... raise ValueError
... yield item
...
>>> list(iter_index(assert_no_value('AABCADEAF', 'B'), 'A'))
Traceback (most recent call last):
...
ValueError

>>> list(sieve(30))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Expand Down