From a69f8a5c9d80b193fce84f5f126d09f4d3108338 Mon Sep 17 00:00:00 2001 From: Ryan Grout Date: Tue, 14 Aug 2018 09:20:05 -0500 Subject: [PATCH] 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. --- toolz/itertoolz.py | 8 +++++++- toolz/tests/test_itertoolz.py | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 71c7ddc0..429f6c30 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -56,7 +56,13 @@ def accumulate(binop, seq, initial=no_default): itertools.accumulate : In standard itertools for Python 3.2+ """ seq = iter(seq) - result = next(seq) if initial == no_default else initial + if initial == no_default: + try: + result = next(seq) + except StopIteration: + return + else: + result = initial yield result for elem in seq: result = binop(result, elem) diff --git a/toolz/tests/test_itertoolz.py b/toolz/tests/test_itertoolz.py index e0a8d17f..8a5c2407 100644 --- a/toolz/tests/test_itertoolz.py +++ b/toolz/tests/test_itertoolz.py @@ -289,6 +289,7 @@ def binop(a, b): start = object() assert list(accumulate(binop, [], start)) == [start] + assert list(accumulate(binop, [])) == [] assert list(accumulate(add, [1, 2, 3], no_default2)) == [1, 3, 6]