mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
gh-118476: Fix corner cases in islice() rough equivalent. (Gh-118559)
This commit is contained in:
parent
fd0ea63f82
commit
c7c9b913c0
1 changed files with 11 additions and 18 deletions
|
|
@ -504,24 +504,17 @@ loops that truncate the stream.
|
||||||
# islice('ABCDEFG', 2, None) → C D E F G
|
# islice('ABCDEFG', 2, None) → C D E F G
|
||||||
# islice('ABCDEFG', 0, None, 2) → A C E G
|
# islice('ABCDEFG', 0, None, 2) → A C E G
|
||||||
s = slice(*args)
|
s = slice(*args)
|
||||||
start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
|
start = 0 if s.start is None else s.start
|
||||||
it = iter(range(start, stop, step))
|
stop = s.stop
|
||||||
try:
|
step = 1 if s.step is None else s.step
|
||||||
nexti = next(it)
|
if start < 0 or (stop is not None and stop < 0) or step <= 0:
|
||||||
except StopIteration:
|
raise ValueError
|
||||||
# Consume *iterable* up to the *start* position.
|
indices = count() if stop is None else range(max(stop, start))
|
||||||
for i, element in zip(range(start), iterable):
|
next_i = start
|
||||||
pass
|
for i, element in zip(indices, iterable):
|
||||||
return
|
if i == next_i:
|
||||||
try:
|
yield element
|
||||||
for i, element in enumerate(iterable):
|
next_i += step
|
||||||
if i == nexti:
|
|
||||||
yield element
|
|
||||||
nexti = next(it)
|
|
||||||
except StopIteration:
|
|
||||||
# Consume to *stop*.
|
|
||||||
for i, element in zip(range(i + 1, stop), iterable):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
.. function:: pairwise(iterable)
|
.. function:: pairwise(iterable)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue