[Python-checkins] gh-106566: Optimize (?!) in regular expressions (GH-106567)

serhiy-storchaka webhook-mailer at python.org
Mon Aug 7 11:10:00 EDT 2023


https://github.com/python/cpython/commit/ed64204716035db58c7e11b78182596aa2d97176
commit: ed64204716035db58c7e11b78182596aa2d97176
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2023-08-07T18:09:56+03:00
summary:

gh-106566: Optimize (?!) in regular expressions (GH-106567)

files:
A Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst
M Lib/re/_parser.py
M Lib/test/test_re.py

diff --git a/Lib/re/_parser.py b/Lib/re/_parser.py
index 22d10ab6e31d3..d00b7e67d5595 100644
--- a/Lib/re/_parser.py
+++ b/Lib/re/_parser.py
@@ -773,8 +773,10 @@ def _parse(source, state, verbose, nested, first=False):
                                            source.tell() - start)
                     if char == "=":
                         subpatternappend((ASSERT, (dir, p)))
-                    else:
+                    elif p:
                         subpatternappend((ASSERT_NOT, (dir, p)))
+                    else:
+                        subpatternappend((FAILURE, ()))
                     continue
 
                 elif char == "(":
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index a6f5af17d7d51..a565cbe1a8d81 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -2362,6 +2362,9 @@ def test_regression_gh94675(self):
                 p.terminate()
                 p.join()
 
+    def test_fail(self):
+        self.assertEqual(re.search(r'12(?!)|3', '123')[0], '3')
+
 
 def get_debug_out(pat):
     with captured_stdout() as out:
diff --git a/Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst b/Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst
new file mode 100644
index 0000000000000..3b88dc7918387
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst
@@ -0,0 +1 @@
+Optimize ``(?!)`` (pattern which alwais fails) in regular expressions.



More information about the Python-checkins mailing list