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
10 changes: 6 additions & 4 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ def _guess_quote_and_delimiter(self, data, delimiters):
# if we see an extra quote between delimiters, we've got a
# double quoted format
dq_regexp = re.compile(
r"((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \
{'delim':re.escape(delim), 'quote':quotechar}, re.MULTILINE)


r"(?:%(delim)s|^) *+%(quote)s" # ,"
r"[^%(quote)s]*+%(quote)s%(quote)s" # the doubled quote
r"(?:%(quote)s%(quote)s|[^%(quote)s]++)*+" # the rest of the field
r"%(quote)s(?:%(delim)s|$)" # ",
% {'delim': re.escape(delim), 'quote': quotechar},
re.MULTILINE)

if dq_regexp.search(data):
doublequote = True
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,13 @@ def test_zero_mode_tie_order_colon_first(self):
sniffer.sniff(sample)


def test_sniff_regex_backtracking(self):
# gh-109638: this artificial sample used to take minutes.
sniffer = csv.Sniffer()
sample = '"",' * 100 + '"' * 100 + '0' + '"' * 100 + '0'
self.assertEqual(sniffer.sniff(sample).delimiter, ',')


class NUL:
def write(s, *args):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix exponential time in :meth:`csv.Sniffer.sniff` for a sample which contains
many quote characters. A doubled quote character is now also detected in
a field which contains the delimiter or a line break.
Loading