From ed64204716035db58c7e11b78182596aa2d97176 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Aug 2023 18:09:56 +0300 Subject: [PATCH] gh-106566: Optimize (?!) in regular expressions (GH-106567) --- Lib/re/_parser.py | 4 +++- Lib/test/test_re.py | 3 +++ .../Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst diff --git a/Lib/re/_parser.py b/Lib/re/_parser.py index 22d10ab6e31..d00b7e67d55 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 a6f5af17d7d..a565cbe1a8d 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -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 index 00000000000..3b88dc79183 --- /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.