]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] Fix iter_index() to work with lists which do not support stop=None. (gh-109306...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 12 Sep 2023 14:23:20 +0000 (07:23 -0700)
committerGitHub <noreply@github.com>
Tue, 12 Sep 2023 14:23:20 +0000 (16:23 +0200)
Fix iter_index() to work with lists which do not support stop=None. (gh-109306)
(cherry picked from commit f2a55fecd063244a5fd09a38f673f0781f8802d1)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Doc/library/itertools.rst

index 5c4d1728234b336c82bc0f155c51703f3a0288a8..7fc1ae913cdba53a8c5ea0220d035c6971e68b98 100644 (file)
@@ -877,6 +877,7 @@ which incur interpreter overhead.
                    yield i
        else:
            # Fast path for sequences
+           stop = len(iterable) if stop is None else stop
            i = start - 1
            try:
                while True:
@@ -1347,6 +1348,16 @@ The following recipes have a more mathematical flavor:
     Traceback (most recent call last):
     ...
     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))
     [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]