Skip to content

Commit 413b9d6

Browse files
Match the body of a quoted field without possessive quantifiers
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>
1 parent 2be8f83 commit 413b9d6

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

Lib/csv.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ def _guess_quote_and_delimiter(self, data, delimiters):
218218
# The body of a quoted field ends at the first quote which is
219219
# not doubled, as it does for a reader. A lazy ".*?" scans to
220220
# the end of the sample instead, from every start: quadratically.
221-
body = r'(?:(?P=quote){2}|(?!(?P=quote)).)*+'
221+
# As an unrolled loop it is unambiguous, so it does not backtrack.
222+
other = r'(?:(?!(?P=quote)).)*'
223+
body = r'%s(?:(?P=quote){2}%s)*' % (other, other)
222224
matches = []
223225
for restr in (r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\'])%s(?P=quote)(?P=delim)', # ,"...",
224226
r'(?:^|\n)(?P<quote>["\'])%s(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # "...",

0 commit comments

Comments
 (0)