Skip to content

Commit 1a420c0

Browse files
gh-106052: Fix bug in the matching of possessive quantifiers
It did not work in the case of a subpattern containing backtraces. Temporary implement possessive quantifiers as equivalent greedy qualifiers in atomic groups.
1 parent 24fb627 commit 1a420c0

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/re/_compiler.py

Lines changed: 7 additions & 0 deletions
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 backtraces, 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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,16 @@ def test_bug_gh91616(self):
23982398
self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer
23992399
self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt"))
24002400

2401+
def test_bug_gh106052(self):
2402+
self.assertEqual(re.match("(?>(?:ab?c)+)", "aca").span(), (0, 2))
2403+
self.assertEqual(re.match("(?:ab?c)++", "aca").span(), (0, 2))
2404+
self.assertEqual(re.match("(?>(?:ab?c)*)", "aca").span(), (0, 2))
2405+
self.assertEqual(re.match("(?:ab?c)*+", "aca").span(), (0, 2))
2406+
self.assertEqual(re.match("(?>(?:ab?c)?)", "a").span(), (0, 0))
2407+
self.assertEqual(re.match("(?:ab?c)?+", "a").span(), (0, 0))
2408+
self.assertEqual(re.match("(?>(?:ab?c){1,3})", "aca").span(), (0, 2))
2409+
self.assertEqual(re.match("(?:ab?c){1,3}+", "aca").span(), (0, 2))
2410+
24012411
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
24022412
def test_regression_gh94675(self):
24032413
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
@@ -2491,6 +2501,7 @@ def test_atomic_group(self):
24912501
17: SUCCESS
24922502
''')
24932503

2504+
@unittest.expectedFailure # gh-106052
24942505
def test_possesive_repeat_one(self):
24952506
self.assertEqual(get_debug_out(r'a?+'), '''\
24962507
POSSESSIVE_REPEAT 0 1
@@ -2503,6 +2514,7 @@ def test_possesive_repeat_one(self):
25032514
12: SUCCESS
25042515
''')
25052516

2517+
@unittest.expectedFailure # gh-106052
25062518
def test_possesive_repeat(self):
25072519
self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\
25082520
POSSESSIVE_REPEAT 0 1
Lines changed: 2 additions & 0 deletions
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 backtraces.

0 commit comments

Comments
 (0)