Skip to content

Commit a69f8a5

Browse files
committed
Fix accumulate for Python 3.7
StopIteration is converted to RuntimeError if it escapes the generator frame. Code updated to reflect recommendations made in PEP479.
1 parent c789e3c commit a69f8a5

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

toolz/itertoolz.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ def accumulate(binop, seq, initial=no_default):
5656
itertools.accumulate : In standard itertools for Python 3.2+
5757
"""
5858
seq = iter(seq)
59-
result = next(seq) if initial == no_default else initial
59+
if initial == no_default:
60+
try:
61+
result = next(seq)
62+
except StopIteration:
63+
return
64+
else:
65+
result = initial
6066
yield result
6167
for elem in seq:
6268
result = binop(result, elem)

toolz/tests/test_itertoolz.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ def binop(a, b):
289289

290290
start = object()
291291
assert list(accumulate(binop, [], start)) == [start]
292+
assert list(accumulate(binop, [])) == []
292293
assert list(accumulate(add, [1, 2, 3], no_default2)) == [1, 3, 6]
293294

294295

0 commit comments

Comments
 (0)