]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-118476: Fix corner cases in islice() rough equivalent. (Gh-118559) (#118587)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 5 May 2024 06:49:19 +0000 (08:49 +0200)
committerGitHub <noreply@github.com>
Sun, 5 May 2024 06:49:19 +0000 (06:49 +0000)
Doc/library/itertools.rst

index e3ce72a2f361404a65171476a21275812e295727..6dd66cb701634f76f209ae1a499ecdcf563c12e6 100644 (file)
@@ -496,24 +496,17 @@ loops that truncate the stream.
           # islice('ABCDEFG', 2, None) → C D E F G
           # islice('ABCDEFG', 0, None, 2) → A C E G
           s = slice(*args)
-          start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
-          it = iter(range(start, stop, step))
-          try:
-              nexti = next(it)
-          except StopIteration:
-              # Consume *iterable* up to the *start* position.
-              for i, element in zip(range(start), iterable):
-                  pass
-              return
-          try:
-              for i, element in enumerate(iterable):
-                  if i == nexti:
-                      yield element
-                      nexti = next(it)
-          except StopIteration:
-              # Consume to *stop*.
-              for i, element in zip(range(i + 1, stop), iterable):
-                  pass
+          start = 0 if s.start is None else s.start
+          stop = s.stop
+          step = 1 if s.step is None else s.step
+          if start < 0 or (stop is not None and stop < 0) or step <= 0:
+              raise ValueError
+          indices = count() if stop is None else range(max(stop, start))
+          next_i = start
+          for i, element in zip(indices, iterable):
+              if i == next_i:
+                  yield element
+                  next_i += step
 
 
 .. function:: pairwise(iterable)