]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix iter_index() to work with lists which do not support stop=None. (gh-109306)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 12 Sep 2023 02:04:28 +0000 (04:04 +0200)
committerGitHub <noreply@github.com>
Tue, 12 Sep 2023 02:04:28 +0000 (04:04 +0200)
Doc/library/itertools.rst

index 3cfc2602fe0694d01021f1cbbb401eecb2d14c7a..5e187aea441bb949237779108beed144252da5c0 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:
@@ -1345,6 +1346,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]