]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106566: Optimize (?!) in regular expressions (GH-106567)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 7 Aug 2023 15:09:56 +0000 (18:09 +0300)
committerGitHub <noreply@github.com>
Mon, 7 Aug 2023 15:09:56 +0000 (18:09 +0300)
Lib/re/_parser.py
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst [new file with mode: 0644]

index 22d10ab6e31d37f1ec2e59c787d9b46d9d1707dc..d00b7e67d55958201175bf2d3598336032ba2806 100644 (file)
@@ -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 == "(":
index a6f5af17d7d51ba18deb9c7786a74918cb7470ff..a565cbe1a8d810e730ceb6aaf61e0d69e57c1da7 100644 (file)
@@ -2362,6 +2362,9 @@ class ReTests(unittest.TestCase):
                 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 (file)
index 0000000..3b88dc7
--- /dev/null
@@ -0,0 +1 @@
+Optimize ``(?!)`` (pattern which alwais fails) in regular expressions.