From 35154f3b348700fbf896069b5f953a529725f32e Mon Sep 17 00:00:00 2001 From: sg3-141-592 <5122866+sg3-141-592@users.noreply.github.com> Date: Sat, 9 Sep 2023 21:05:53 +0000 Subject: [PATCH 1/4] Performance improvements for csv doublequote regex --- Lib/csv.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/csv.py b/Lib/csv.py index 77f30c8d2b1f61d..74048ffc063e3a0 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -270,8 +270,9 @@ def _guess_quote_and_delimiter(self, data, delimiters): # 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 dq_regexp = re.compile( - r"((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \ + r"((%(delim)s)|^)\W*%(quote)s(?=(?P[^%(delim)s%(quote)s\n]*))(?P=zero)%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \ {'delim':re.escape(delim), 'quote':quotechar}, re.MULTILINE) From 16d9612d2d5c0e3af00636e864853531d1eeb010 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 21 Sep 2023 07:04:44 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst diff --git a/Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst b/Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst new file mode 100644 index 000000000000000..204513c164c77d8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst @@ -0,0 +1 @@ +Fix csv.Sniffer() having very long execution time for invalid strings. From 2b29051cf0465b35445dc04d36a47c446ab58ac1 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 22 Sep 2023 21:27:58 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Revert=20"=F0=9F=93=9C=F0=9F=A4=96=20Added?= =?UTF-8?q?=20by=20blurb=5Fit."?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 16d9612d2d5c0e3af00636e864853531d1eeb010. --- .../next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst diff --git a/Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst b/Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst deleted file mode 100644 index 204513c164c77d8..000000000000000 --- a/Misc/NEWS.d/next/Library/2023-09-21-07-04-43.gh-issue-109638.AjI_OD.rst +++ /dev/null @@ -1 +0,0 @@ -Fix csv.Sniffer() having very long execution time for invalid strings. From 026564124e8d4556753af5c1371231f86d35b9eb Mon Sep 17 00:00:00 2001 From: sg3-141-592 <5122866+sg3-141-592@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:26:39 +0000 Subject: [PATCH 4/4] Converting zero-length lookahead assertions to atomic group --- Lib/csv.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/csv.py b/Lib/csv.py index 74048ffc063e3a0..ce65fee45320cd6 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -270,9 +270,8 @@ def _guess_quote_and_delimiter(self, data, delimiters): # 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 dq_regexp = re.compile( - r"((%(delim)s)|^)\W*%(quote)s(?=(?P[^%(delim)s%(quote)s\n]*))(?P=zero)%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \ + 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)