Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,19 +483,29 @@ def _try_dialect(self, lines, cut, delimiter, quotechar, escapechar):
If the sample cannot be parsed to the end (for example it is
cut off in the middle of a quoted field, or the combination
does not fit the sample), the rows parsed so far are counted.
Rows exceeding the field size limit are skipped so that later
rows can still contribute to the guess.
The last row is not counted if *cut* is true: the sample can
be cut off in the middle of it.
"""
rows = []
try:
rows.extend(map(len, self._make_reader(lines, delimiter,
quotechar, escapechar)))
except Error:
# The row which failed to parse is not counted.
pass
else:
if cut and len(rows) > 1:
rows.pop()
reader = iter(self._make_reader(lines, delimiter, quotechar, escapechar))
parse_error = False
while True:
try:
row = next(reader)
except StopIteration:
break
except Error as error:
if not str(error).startswith("field larger than field limit"):
# The sample doesn't fit this dialect.
parse_error = True
break
# Skip the oversized row and let later rows provide evidence.
continue
rows.append(len(row))
if not parse_error and cut and len(rows) > 1:
rows.pop()
if 0 in rows:
# Blank lines produce empty rows.
rows = [nfields for nfields in rows if nfields]
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,17 @@ def test_sniff(self):
self.assertEqual(dialect.quotechar, "'")
self.assertIs(dialect.skipinitialspace, False)

def test_sniff_field_larger_than_field_size_limit(self):
sniffer = csv.Sniffer()
sample = 'a,"' + 'x' * 200000 + '",b\n' + 'c,d,e\n' * 50
limit = csv.field_size_limit(100)
try:
dialect = sniffer.sniff(sample)
self.assertEqual(dialect.delimiter, ',')
self.assertEqual(csv.field_size_limit(), 100)
finally:
csv.field_size_limit(limit)

def test_delimiters(self):
sniffer = csv.Sniffer()
dialect = sniffer.sniff(self.sample3)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow :meth:`csv.Sniffer.sniff` to process fields larger than the configured
:func:`csv.field_size_limit` without changing the configured limit.
Loading