[3.12] Fix iter_index() to work with lists which do not support stop=None. (gh-109306) (#109310)

Fix iter_index() to work with lists which do not support stop=None. (gh-109306)
(cherry picked from commit f2a55fecd0)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2023-09-12 07:23:20 -07:00 committed by GitHub
parent 7479a7aca2
commit 778d094126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -877,6 +877,7 @@ which incur interpreter overhead.
yield i yield i
else: else:
# Fast path for sequences # Fast path for sequences
stop = len(iterable) if stop is None else stop
i = start - 1 i = start - 1
try: try:
while True: while True:
@ -1347,6 +1348,16 @@ The following recipes have a more mathematical flavor:
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError ValueError
>>> # Verify that both paths can find identical NaN values
>>> x = float('NaN')
>>> y = float('NaN')
>>> list(iter_index([0, x, x, y, 0], x))
[1, 2]
>>> list(iter_index(iter([0, x, x, y, 0]), x))
[1, 2]
>>> # Test list input. Lists do not support None for the stop argument
>>> list(iter_index(list('AABCADEAF'), 'A'))
[0, 1, 4, 7]
>>> list(sieve(30)) >>> list(sieve(30))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]