From bcd30a6b9e3f6782bf663b89470ee6ff0c4d8168 Mon Sep 17 00:00:00 2001 From: James McKinney Date: Sat, 23 Jan 2016 11:56:43 -0500 Subject: [PATCH 1/2] Add test demonstrating #469 --- tests/test_table.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_table.py b/tests/test_table.py index 44a3d4267..8a8e5cf72 100644 --- a/tests/test_table.py +++ b/tests/test_table.py @@ -91,6 +91,10 @@ def test_from_csv_no_inference(self): def test_from_csv_empty_file(self): table.Table.from_csv(six.StringIO('\n\n')) + def test_from_csv_dev_null(self): + with open('/dev/null', 'r') as f: + table.Table.from_csv(f) + def test_to_csv(self): raise SkipTest with open('examples/testfixed_converted.csv', 'r') as f: From 6ed9704b208ae27ae98fd322850df8a82087339f Mon Sep 17 00:00:00 2001 From: James McKinney Date: Sat, 23 Jan 2016 11:58:09 -0500 Subject: [PATCH 2/2] Fix #469 by catching StopIteration --- csvkit/table.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/csvkit/table.py b/csvkit/table.py index 3918d7188..9c9736240 100644 --- a/csvkit/table.py +++ b/csvkit/table.py @@ -209,28 +209,26 @@ def from_csv(cls, f, name='from_csv_table', snifflimit=None, column_ids=None, bl f = six.StringIO(contents) rows = agate.reader(f, **kwargs) - if no_header_row: - # Peek at a row to infer column names from - row = next(rows) + try: + if no_header_row: + # Peek at a row to infer column names from, and put it back on top + row = next(rows) + rows = itertools.chain([row], rows) + headers = make_default_headers(len(row)) + else: + headers = next(rows) + except StopIteration: + # The file is `/dev/null`. + headers = [] + pass - headers = make_default_headers(len(row)) + if no_header_row or column_ids: column_ids = parse_column_identifiers(column_ids, headers, zero_based) headers = [headers[c] for c in column_ids] - data_columns = [[] for c in headers] - - # Put row back on top - rows = itertools.chain([row], rows) else: - headers = next(rows) - - if column_ids: - column_ids = parse_column_identifiers(column_ids, headers, zero_based) - headers = [headers[c] for c in column_ids] - else: - column_ids = range(len(headers)) - - data_columns = [[] for c in headers] + column_ids = range(len(headers)) + data_columns = [[] for c in headers] width = len(data_columns) for i, row in enumerate(rows):