]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-106052: Fix bug in the matching of possessive quantifiers (GH-106515) ...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 9 Aug 2023 06:15:27 +0000 (09:15 +0300)
committerGitHub <noreply@github.com>
Wed, 9 Aug 2023 06:15:27 +0000 (06:15 +0000)
It did not work in the case of a subpattern containing backtracking.

Temporary implement possessive quantifiers as equivalent greedy qualifiers
in atomic groups.
(cherry picked from commit 7b6e34e5baeb4162815ffa4d943b09a58e3f6580)

Lib/re/_compiler.py
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2023-07-07-14-52-31.gh-issue-106052.ak8nbs.rst [new file with mode: 0644]

index d8e0d2fdefdccad7150fe369a673733a60754ca1..e30740b9c30b0e8cc95d907195f0b99d7d71cfae 100644 (file)
@@ -100,6 +100,13 @@ def _compile(code, pattern, flags):
                 emit(ANY_ALL)
             else:
                 emit(ANY)
+        elif op is POSSESSIVE_REPEAT:
+            # gh-106052: Possessive quantifiers do not work when the
+            # subpattern contains backtracking, i.e. "(?:ab?c)*+".
+            # Implement it as equivalent greedy qualifier in atomic group.
+            p = [(MAX_REPEAT, av)]
+            p = [(ATOMIC_GROUP, p)]
+            _compile(code, p, flags)
         elif op in REPEATING_CODES:
             if flags & SRE_FLAG_TEMPLATE:
                 raise error("internal: unsupported template operator %r" % (op,))
index 38d6db7c4091b417238489fcbb1a50f17fc47288..d2736dae20b1b4252ba64030d08a8f3868a345e1 100644 (file)
@@ -2396,6 +2396,16 @@ class ReTests(unittest.TestCase):
         self.assertTrue(template_re1.match('ahoy'))
         self.assertFalse(template_re1.match('nope'))
 
+    def test_bug_gh106052(self):
+        self.assertEqual(re.match("(?>(?:ab?c)+)", "aca").span(), (0, 2))
+        self.assertEqual(re.match("(?:ab?c)++", "aca").span(), (0, 2))
+        self.assertEqual(re.match("(?>(?:ab?c)*)", "aca").span(), (0, 2))
+        self.assertEqual(re.match("(?:ab?c)*+", "aca").span(), (0, 2))
+        self.assertEqual(re.match("(?>(?:ab?c)?)", "a").span(), (0, 0))
+        self.assertEqual(re.match("(?:ab?c)?+", "a").span(), (0, 0))
+        self.assertEqual(re.match("(?>(?:ab?c){1,3})", "aca").span(), (0, 2))
+        self.assertEqual(re.match("(?:ab?c){1,3}+", "aca").span(), (0, 2))
+
     @unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
     def test_regression_gh94675(self):
         pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
@@ -2492,6 +2502,7 @@ ATOMIC_GROUP
 17: SUCCESS
 ''')
 
+    @unittest.expectedFailure  # gh-106052
     def test_possesive_repeat_one(self):
         self.assertEqual(get_debug_out(r'a?+'), '''\
 POSSESSIVE_REPEAT 0 1
@@ -2504,6 +2515,7 @@ POSSESSIVE_REPEAT 0 1
 12: SUCCESS
 ''')
 
+    @unittest.expectedFailure  # gh-106052
     def test_possesive_repeat(self):
         self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\
 POSSESSIVE_REPEAT 0 1
diff --git a/Misc/NEWS.d/next/Library/2023-07-07-14-52-31.gh-issue-106052.ak8nbs.rst b/Misc/NEWS.d/next/Library/2023-07-07-14-52-31.gh-issue-106052.ak8nbs.rst
new file mode 100644 (file)
index 0000000..f2d4c2f
--- /dev/null
@@ -0,0 +1,2 @@
+:mod:`re` module: fix the matching of possessive quantifiers in the case of
+a subpattern containing backtracking.