From 44d16732abcbb177080068379bc6a8243f3ed360 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Sun, 28 Apr 2024 00:31:05 -0400 Subject: [PATCH] fix: Don't error on empty iterators 1db7277 --- CHANGELOG.rst | 2 +- agate/table/__init__.py | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e84bdc5..c6ba12d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,7 +1,7 @@ 1.10.1 - April 27, 2024 ----------------------- -- fix: Version 1.10.0 seeks to the file's beginning, instead of to the original offset. +- fix: Version 1.10.0 errors on empty tables and seeks to the file's beginning, instead of to the original offset. - fix: :meth:`.Number.csvify` returns a ``Decimal`` (or ``None``), instead of ``str``. :meth:`.Table.to_csv` with ``quoting=csv.QUOTE_NONNUMERIC`` now works. 1.10.0 - April 27, 2024 diff --git a/agate/table/__init__.py b/agate/table/__init__.py index 1d05503..542e861 100644 --- a/agate/table/__init__.py +++ b/agate/table/__init__.py @@ -78,18 +78,17 @@ def __init__(self, rows, column_names=None, column_types=None, row_names=None, _ # Validate column names if column_names: self._column_names = utils.deduplicate(column_names, column_names=True) - elif rows: + else: + rows = iter(rows) try: - first_row = rows[0] - except TypeError: - # rows is an iterator. first_row = next(rows) + except StopIteration: + self._column_names = tuple() + else: rows = chain([first_row], rows) - self._column_names = tuple(utils.letter_name(i) for i in range(len(first_row))) - warnings.warn('Column names not specified. "%s" will be used as names.' % str(self._column_names), - RuntimeWarning, stacklevel=2) - else: - self._column_names = tuple() + self._column_names = tuple(utils.letter_name(i) for i in range(len(first_row))) + warnings.warn('Column names not specified. "%s" will be used as names.' % str(self._column_names), + RuntimeWarning, stacklevel=2) len_column_names = len(self._column_names) @@ -108,7 +107,7 @@ def __init__(self, rows, column_names=None, column_types=None, row_names=None, _ raise ValueError('Column types must be instances of DataType.') if isinstance(column_types, TypeTester): - # In case rows is an iterator, in which case need to read it all into memory. + # Need to read all rows into memory. rows = tuple(rows) self._column_types = column_types.run(rows, self._column_names) else: