batch: use builtin next() and catch StopIteration (#38781)

Catching StopIteration is for 3.7 compatibility:
https://www.python.org/dev/peps/pep-0479/
This commit is contained in:
Emmanuel Cazenave 2020-01-13 15:14:58 +01:00
parent f44ffec80f
commit c9ccc87626
2 changed files with 11 additions and 1 deletions

View File

@ -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

View File

@ -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]