Skip to content

Commit

Permalink
Merge pull request #510 from onyxfish/469
Browse files Browse the repository at this point in the history
Don't error when reading from /dev/null, fixes #469
  • Loading branch information
James McKinney committed Jan 25, 2016
2 parents 2bc8409 + 6ed9704 commit 941555b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
32 changes: 15 additions & 17 deletions csvkit/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 941555b

Please sign in to comment.