Skip to content

[3.10] gh-98820: Fix quadratic time in csv.Sniffer for quoted fields (GH-154867) - #155539

Merged
pablogsal merged 2 commits into
python:3.10from
serhiy-storchaka:backport-3.10-gh98820
Aug 11, 2026
Merged

[3.10] gh-98820: Fix quadratic time in csv.Sniffer for quoted fields (GH-154867)#155539
pablogsal merged 2 commits into
python:3.10from
serhiy-storchaka:backport-3.10-gh98820

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Manual backport of #154867, cherry-picked from the 3.12 backport (#155166), with one 3.10-only change in the second commit.

Possessive quantifiers were added in 3.11, so (?:(?P=quote){2}|(?!(?P=quote)).)*+ does not compile here. The body is matched as an unrolled loop instead; since the quote character is a backreference, the "not a quote" atom needs a lookahead:

other = r'(?:(?!(?P=quote)).)*'
body = r'%s(?:(?P=quote){2}%s)*' % (other, other)

Every position matches only one of the two alternatives, and each iteration of the outer loop consumes the two quote characters, so it does not backtrack.

Checked against the 3.11 version of the file: no difference in the sniffed dialect on 90000 samples, and the same timings — sniffing '"abcdefghijklmnopqrstuvwxyz"\n' * n with delimiters=',:|\t' takes 0.147, 0.298 and 0.602 s for n = 2500, 5000 and 10000, against 0.142, 0.286 and 0.569 s on 3.11.

miss-islington and others added 2 commits August 11, 2026 09:44
…ields (pythonGH-154867)

(cherry picked from commit b30c7fa)
(cherry picked from commit 39dac55)
They were added in 3.11.  An unrolled loop is unambiguous at every
position, so it does not backtrack either.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@bedevere-app bedevere-app Bot added the type-security A security issue label Aug 11, 2026
@pablogsal
pablogsal merged commit fd78b56 into python:3.10 Aug 11, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-security A security issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants