From 96cdc78218fe97174c8c7ddf496c44e39388c5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Poni=C5=84ski?= Date: Tue, 8 Feb 2022 18:50:36 +0100 Subject: [PATCH] Fix iterrowslice to conform with Python 3.7 PEP 479 which transforms StopIteration in generators to RuntimeError exceptions --- docs/changes.rst | 3 +++ petl/test/transform/test_basics.py | 15 +++++++++++++++ petl/transform/basics.py | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 389bb091..bc08fad8 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -4,6 +4,9 @@ Changes Version 1.7.8 ------------- +* Fix iterrowslice() to conform with PEP 479 + By :user:`arturponinski`, :issue:`575`. + * Cleanup and unclutter old and unused files in repository By :user:`juarezr`, :issue:`606`. diff --git a/petl/test/transform/test_basics.py b/petl/test/transform/test_basics.py index 17412eff..2424dab6 100644 --- a/petl/test/transform/test_basics.py +++ b/petl/test/transform/test_basics.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, print_function, division +import pytest from petl.test.helpers import ieq from petl.util import expr, empty, coalesce @@ -460,6 +461,20 @@ def test_head(): ieq(expect, table2) +def test_head_raises_stop_iteration_for_empty_table(): + table = iter(head([])) + with pytest.raises(StopIteration): + next(table) # header + + +def test_head_raises_stop_iteration_for_header_only(): + table1 = (('foo', 'bar', 'baz'),) + table = iter(head(table1)) + next(table) # header + with pytest.raises(StopIteration): + next(table) + + def test_tail(): table1 = (('foo', 'bar'), diff --git a/petl/transform/basics.py b/petl/transform/basics.py index 5142d735..8627e81a 100644 --- a/petl/transform/basics.py +++ b/petl/transform/basics.py @@ -727,7 +727,10 @@ def __iter__(self): def iterrowslice(source, sliceargs): it = iter(source) - yield tuple(next(it)) # fields + try: + yield tuple(next(it)) # fields + except StopIteration: + return for row in islice(it, *sliceargs): yield tuple(row)