Skip to content

Commit 063d455

Browse files
[3.12] gh-98820: Fix quadratic time in csv.Sniffer for quoted fields (GH-154867) (#155166)
(cherry picked from commit b30c7fa) Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
1 parent d9d98e0 commit 063d455

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

Lib/csv.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,16 @@ def _guess_quote_and_delimiter(self, data, delimiters):
222222
this way.
223223
"""
224224

225+
# The body of a quoted field ends at the first quote which is
226+
# not doubled, as it does for a reader. A lazy ".*?" scans to
227+
# the end of the sample instead, from every start: quadratically.
228+
body = r'(?:(?P=quote){2}|(?!(?P=quote)).)*+'
225229
matches = []
226-
for restr in (r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
227-
r'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # ".*?",
228-
r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)', # ,".*?"
229-
r'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space)
230-
regexp = re.compile(restr, re.DOTALL | re.MULTILINE)
230+
for restr in (r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\'])%s(?P=quote)(?P=delim)', # ,"...",
231+
r'(?:^|\n)(?P<quote>["\'])%s(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # "...",
232+
r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\'])%s(?P=quote)(?:$|\n)', # ,"..."
233+
r'(?:^|\n)(?P<quote>["\'])%s(?P=quote)(?:$|\n)'): # "..." (no delim, no space)
234+
regexp = re.compile(restr % body, re.DOTALL | re.MULTILINE)
231235
matches = regexp.findall(data)
232236
if matches:
233237
break

Lib/test/test_csv.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,13 @@ def test_sniff_space_delimiter(self):
14151415
self.assertEqual(dialect.delimiter, ' ')
14161416
self.assertIs(dialect.doublequote, False)
14171417

1418+
def test_sniff_quoted_single_column(self):
1419+
# gh-98820: this sample used to take minutes.
1420+
sniffer = csv.Sniffer()
1421+
sample = '"abcdefghijklmnopqrstuvwxyz"\n' * 10000
1422+
with self.assertRaisesRegex(csv.Error, "Could not determine delimiter"):
1423+
sniffer.sniff(sample, delimiters=',:|\t')
1424+
14181425

14191426
class NUL:
14201427
def write(s, *args):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix quadratic time in :meth:`csv.Sniffer.sniff` for a sample which contains
2+
quoted fields, in particular for a single column of quoted fields.

0 commit comments

Comments
 (0)