fix: _cut_iter yields wrong-length parts when step > n

partition()/chunks() on an iterator (_cut_iter) only correctly skipped
step items between parts when step <= n. For step > n, slicing an
n-length pool by pool[step:] silently clips to an empty list, then
islice(it, step) pulled step (not n) fresh items into the pool instead
of first discarding the step - n items between parts. This produced
wrong-length, wrong-content chunks that diverged from the Sequence
code path (_cut_seq) for the same input, e.g. list(range(10)) vs
iter(range(10)) with n=2, step=3.

Branch on step < n to keep the existing sliding-window logic, and for
step >= n explicitly discard the gap before taking the next pool.
This commit is contained in:
dualfroz committed 2026-09-04 04:48:04 +02:00
1 parent f15f2b80fb
commit a12abd3a5e
2 files changed
+18 -2

No files matched your search

+6 -2
View File
@@ -363,8 +363,12 @@ def _cut_iter(drop_tail, n, step, seq):
if len(pool) < n:
break
yield pool
pool = pool[step:]
pool.extend(islice(it, step))
if step < n:
pool = pool[step:]
pool.extend(islice(it, step))
else:
take(step - n, it) # discard the gap between parts
pool = take(n, it)
if not drop_tail:
for item in _cut_seq(drop_tail, n, step, pool):
yield item