Skip to content

Commit 95bd4ae

Browse files
committed
1 parent 7734845 commit 95bd4ae

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ jobs:
3131
PYTHONIOENCODING: utf-8
3232
PYTHONUTF8: 1
3333
run: pytest --cov agate
34+
- name: Read from stdin
35+
run: python -c 'import sys; import agate; agate.Table.from_csv(sys.stdin, sniff_limit=1)' < examples/test.csv
36+
- name: Read from pipe
37+
run: printf 'a,b,c\n1,2,3' | python -c 'import sys; import agate; agate.Table.from_csv(sys.stdin, sniff_limit=1)'
3438
- run: python charts.py
3539
- env:
3640
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Unreleased
2+
----------
3+
4+
- fix: Version 1.10.0 errors on piped data.
5+
16
1.10.1 - April 28, 2024
27
-----------------------
38

agate/table/from_csv.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import io
12
import itertools
2-
from io import StringIO
3+
import sys
34

45

56
@classmethod
@@ -63,14 +64,20 @@ def from_csv(cls, path, column_names=None, column_types=None, row_names=None, sk
6364

6465
if sniff_limit is None:
6566
# Reads to the end of the tile, but avoid reading the file twice.
66-
handle = StringIO(f.read())
67-
kwargs['dialect'] = csv.Sniffer().sniff(handle.getvalue())
67+
handle = io.StringIO(f.read())
68+
sample = handle.getvalue()
6869
elif sniff_limit > 0:
69-
offset = f.tell()
70+
if f == sys.stdin:
71+
buffered = io.BufferedReader(sys.stdin.buffer)
72+
handle = io.TextIOWrapper(buffered, encoding=encoding)
73+
sample = buffered.peek(sniff_limit).decode(encoding) # reads *bytes*
74+
else:
75+
offset = f.tell()
76+
sample = f.read(sniff_limit) # reads *characters*
77+
f.seek(offset) # can't do f.seek(-sniff_limit, os.SEEK_CUR) unless file was opened in binary mode
7078

71-
# Reads only the start of the file.
72-
kwargs['dialect'] = csv.Sniffer().sniff(f.read(sniff_limit))
73-
f.seek(offset)
79+
if sniff_limit is None or sniff_limit > 0:
80+
kwargs['dialect'] = csv.Sniffer().sniff(sample)
7481

7582
reader = csv.reader(handle, header=header, **kwargs)
7683

0 commit comments

Comments
 (0)