gh-109638: Fix for significant backtracking in csv.Sniffer#109639
gh-109638: Fix for significant backtracking in csv.Sniffer#109639sg3-141-592 wants to merge 5 commits into
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This reverts commit 16d9612.
|
|
||
| # if we see an extra quote between delimiters, we've got a | ||
| # double quoted format | ||
| # in future Python versions this zero width look-ahead assert can be replaced with atomic groups |
There was a problem hiding this comment.
Please may you explain this comment?
There was a problem hiding this comment.
Sure, this zero-width lookahead assertion change in the Regex can be done with an atomic group which is cleaner and more concise.
# Current change
(,|^)\W*"(?=(?P<zero>[^,|"\n]*))(?P=zero)"[^,|\n]*"\W*(,|$)
# Atomic Group
(,|^)\W*"(?>[^,|"\n]*)"[^,|\n]*"\W*(,|$)
But atomic groups are only supported in Python 3.11 onwards so I avoided using them here.
There was a problem hiding this comment.
Thanks Sean. This PR (if merged) would be part of Python 3.13, so let's use the better atomic group method.
A
There was a problem hiding this comment.
Sure, I've switched us to the simpler atomic group setup. Performance is identical to the previous fix.
aterrel
left a comment
There was a problem hiding this comment.
This looks like a good quick fix for the problem.
Ultimately though, these regexs are hard to read and cause a few problems with lists and other items. I think we should be thinking about how to replace the sniffer to have a higher accuracy. (See https://github.com/ws-garcia/CSVsniffer which shows that only 67.54% accuracy). I've posted to dpo on this topic here: https://discuss.python.org/t/rewrite-csv-sniffer
|
This PR is stale because it has been open for 30 days with no activity. |
#109638 it is possible to get significant backtracking in
csv.Sniffer()inside the doublequote checking regex. This change introduces a zero-length lookahead assertion to reduce the amount of backtracking.This yields a significant improvement in testing