Skip to content

Commit 3bb43b7

Browse files
[3.12] gh-106052: Fix bug in the matching of possessive quantifiers (GH-106515) (#107796)
[3.12] gh-106052: Fix bug in the matching of possessive quantifiers (gh-106515) 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 7b6e34e)
1 parent aa2ecef commit 3bb43b7

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/re/_compiler.py

+7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def _compile(code, pattern, flags):
100100
emit(ANY_ALL)
101101
else:
102102
emit(ANY)
103+
elif op is POSSESSIVE_REPEAT:
104+
# gh-106052: Possessive quantifiers do not work when the
105+
# subpattern contains backtracking, i.e. "(?:ab?c)*+".
106+
# Implement it as equivalent greedy qualifier in atomic group.
107+
p = [(MAX_REPEAT, av)]
108+
p = [(ATOMIC_GROUP, p)]
109+
_compile(code, p, flags)
103110
elif op in REPEATING_CODES:
104111
if flags & SRE_FLAG_TEMPLATE:
105112
raise error("internal: unsupported template operator %r" % (op,))

Lib/test/test_re.py

+12
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,16 @@ def test_template_function_and_flag_is_deprecated(self):
23652365
self.assertTrue(template_re1.match('ahoy'))
23662366
self.assertFalse(template_re1.match('nope'))
23672367

2368+
def test_bug_gh106052(self):
2369+
self.assertEqual(re.match("(?>(?:ab?c)+)", "aca").span(), (0, 2))
2370+
self.assertEqual(re.match("(?:ab?c)++", "aca").span(), (0, 2))
2371+
self.assertEqual(re.match("(?>(?:ab?c)*)", "aca").span(), (0, 2))
2372+
self.assertEqual(re.match("(?:ab?c)*+", "aca").span(), (0, 2))
2373+
self.assertEqual(re.match("(?>(?:ab?c)?)", "a").span(), (0, 0))
2374+
self.assertEqual(re.match("(?:ab?c)?+", "a").span(), (0, 0))
2375+
self.assertEqual(re.match("(?>(?:ab?c){1,3})", "aca").span(), (0, 2))
2376+
self.assertEqual(re.match("(?:ab?c){1,3}+", "aca").span(), (0, 2))
2377+
23682378
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
23692379
def test_regression_gh94675(self):
23702380
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
@@ -2461,6 +2471,7 @@ def test_atomic_group(self):
24612471
17: SUCCESS
24622472
''')
24632473

2474+
@unittest.expectedFailure # gh-106052
24642475
def test_possesive_repeat_one(self):
24652476
self.assertEqual(get_debug_out(r'a?+'), '''\
24662477
POSSESSIVE_REPEAT 0 1
@@ -2473,6 +2484,7 @@ def test_possesive_repeat_one(self):
24732484
12: SUCCESS
24742485
''')
24752486

2487+
@unittest.expectedFailure # gh-106052
24762488
def test_possesive_repeat(self):
24772489
self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\
24782490
POSSESSIVE_REPEAT 0 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`re` module: fix the matching of possessive quantifiers in the case of
2+
a subpattern containing backtracking.

0 commit comments

Comments
 (0)