-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
[3.15] gh-109638: Avoid pathological backtracking in csv.Sniffer (GH-153694) #154865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Punisheroot
wants to merge
3
commits into
python:3.15
from
Punisheroot:perf/js/avoid-csv-sniffer-backtracking
+151
−10
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -332,20 +332,59 @@ def _guess_quote_and_delimiter(self, data, delimiters): | |||||
| delim = '' | ||||||
| skipinitialspace = 0 | ||||||
|
|
||||||
| # 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) | ||||||
| doublequote = self._detect_doublequote(data, delim, quotechar) | ||||||
|
|
||||||
| return (quotechar, doublequote, delim, skipinitialspace) | ||||||
|
|
||||||
|
|
||||||
| if dq_regexp.search(data): | ||||||
| doublequote = True | ||||||
| else: | ||||||
| doublequote = False | ||||||
| def _detect_doublequote(self, data, delimiter, quotechar): | ||||||
| """ | ||||||
| Return whether a doubled quote occurs inside a quoted field. | ||||||
|
|
||||||
| return (quotechar, doublequote, delim, skipinitialspace) | ||||||
| The first regexp is a fast, linear pre-filter. Since it is not | ||||||
| anchored, a match can start at a delimiter inside another quoted | ||||||
| field. The second regexp rules out well-formed input without doubled | ||||||
| quotes. Both regexps use possessive repetition to avoid backtracking. | ||||||
| """ | ||||||
| import re | ||||||
|
|
||||||
| escaped_delimiter = re.escape(delimiter) | ||||||
| escaped_quote = re.escape(quotechar) | ||||||
| values = {'delim': escaped_delimiter, 'quote': escaped_quote} | ||||||
| if delimiter: | ||||||
| # Spaces after a space delimiter are delimiters, not padding. | ||||||
| values['space'] = '' if delimiter == ' ' else ' *+' | ||||||
| candidate = re.compile( | ||||||
| r"(?:%(delim)s|\r|^)%(space)s%(quote)s" | ||||||
| r"[^%(quote)s]*+%(quote)s%(quote)s" | ||||||
| r"(?:%(quote)s%(quote)s|[^%(quote)s]++)*+" | ||||||
| r"%(quote)s(?:%(delim)s|(?=\r)|$)" | ||||||
| % values, re.MULTILINE) | ||||||
| separator = rf"(?:{escaped_delimiter}|\r\n|\r|\n)" | ||||||
| plain = ( | ||||||
| rf"(?! *+{escaped_quote})" | ||||||
| rf"[^{escaped_delimiter}\r\n]*+") | ||||||
| else: | ||||||
| # An empty delimiter must not create a zero-width alternative | ||||||
| # which makes re.search() retry the pattern at every position. | ||||||
| candidate = re.compile( | ||||||
| r"(?:[\r\n]|\A) *+%(quote)s" | ||||||
| r"[^%(quote)s]*+%(quote)s%(quote)s" | ||||||
| r"(?:%(quote)s%(quote)s|[^%(quote)s]++)*+" | ||||||
| r"%(quote)s(?=[\r\n]|\Z)" | ||||||
| % values, re.MULTILINE) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can skip this because there's no
Suggested change
|
||||||
| separator = r"(?:\r\n|\r|\n)" | ||||||
| plain = rf"(?! *+{escaped_quote})[^\r\n]*+" | ||||||
|
|
||||||
| if candidate.search(data) is None: | ||||||
| return False | ||||||
|
|
||||||
| quoted = ( | ||||||
| rf" *+{escaped_quote}[^{escaped_quote}]*+{escaped_quote}") | ||||||
| field = rf"(?:{quoted}|{plain})" | ||||||
| without_doublequote = re.compile( | ||||||
| rf"\A{field}(?:{separator}{field})*+\Z") | ||||||
| return without_doublequote.match(data) is None | ||||||
|
|
||||||
|
|
||||||
| def _guess_delimiter(self, data, delimiters): | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-07-29-13-00-00.gh-issue-109638.SnifF1.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Prevent pathological regular expression backtracking in :class:`csv.Sniffer` | ||
| when detecting doubled quote characters. Improve detection for quoted fields, | ||
| avoid matching across quoted fields, and handle CRLF or CR record delimiters. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be
lazy import reat the top, along with the other in this file.