From c9ccc8762668ee46eeb24475c2921661137cc826 Mon Sep 17 00:00:00 2001 From: Emmanuel Cazenave Date: Mon, 13 Jan 2020 15:14:58 +0100 Subject: [PATCH] batch: use builtin next() and catch StopIteration (#38781) Catching StopIteration is for 3.7 compatibility: https://www.python.org/dev/peps/pep-0479/ --- passerelle/utils/__init__.py | 5 ++++- tests/test_utils_batch.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/test_utils_batch.py diff --git a/passerelle/utils/__init__.py b/passerelle/utils/__init__.py index c4be935b..3633bdec 100644 --- a/passerelle/utils/__init__.py +++ b/passerelle/utils/__init__.py @@ -355,7 +355,10 @@ def batch(iterable, size): batchiter = islice(sourceiter, size) # call next() at least one time to advance, if the caller does not # consume the returned iterators, sourceiter will never be exhausted. - yield chain([batchiter.next()], batchiter) + try: + yield chain([next(batchiter)], batchiter) + except StopIteration: + return # legacy import, other modules keep importing to_json from passerelle.utils from .jsonresponse import to_json diff --git a/tests/test_utils_batch.py b/tests/test_utils_batch.py new file mode 100644 index 00000000..da146431 --- /dev/null +++ b/tests/test_utils_batch.py @@ -0,0 +1,7 @@ +from passerelle.utils import batch + + +def test_batch(): + data = range(10) + for i, d in enumerate(batch(data, 1)): + assert list(d) == [i]