Skip to content

Commit 7b6e34e

Browse files
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.
1 parent 7350738 commit 7b6e34e

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 _simple(av[2]):
105112
emit(REPEATING_CODES[op][2])

Lib/test/test_re.py

+12
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,16 @@ def test_bug_gh91616(self):
23422342
self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer
23432343
self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt"))
23442344

2345+
def test_bug_gh106052(self):
2346+
self.assertEqual(re.match("(?>(?:ab?c)+)", "aca").span(), (0, 2))
2347+
self.assertEqual(re.match("(?:ab?c)++", "aca").span(), (0, 2))
2348+
self.assertEqual(re.match("(?>(?:ab?c)*)", "aca").span(), (0, 2))
2349+
self.assertEqual(re.match("(?:ab?c)*+", "aca").span(), (0, 2))
2350+
self.assertEqual(re.match("(?>(?:ab?c)?)", "a").span(), (0, 0))
2351+
self.assertEqual(re.match("(?:ab?c)?+", "a").span(), (0, 0))
2352+
self.assertEqual(re.match("(?>(?:ab?c){1,3})", "aca").span(), (0, 2))
2353+
self.assertEqual(re.match("(?:ab?c){1,3}+", "aca").span(), (0, 2))
2354+
23452355
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
23462356
def test_regression_gh94675(self):
23472357
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
@@ -2441,6 +2451,7 @@ def test_atomic_group(self):
24412451
17: SUCCESS
24422452
''')
24432453

2454+
@unittest.expectedFailure # gh-106052
24442455
def test_possesive_repeat_one(self):
24452456
self.assertEqual(get_debug_out(r'a?+'), '''\
24462457
POSSESSIVE_REPEAT 0 1
@@ -2453,6 +2464,7 @@ def test_possesive_repeat_one(self):
24532464
12: SUCCESS
24542465
''')
24552466

2467+
@unittest.expectedFailure # gh-106052
24562468
def test_possesive_repeat(self):
24572469
self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\
24582470
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)