Skip to content

Commit

Permalink
gh-106052: Fix bug in the matching of possessive quantifiers (gh-106515)
Browse files Browse the repository at this point in the history
It did not work in the case of a subpattern containing backtracking.

Temporary implement possessive quantifiers as equivalent greedy qualifiers
in atomic groups.
  • Loading branch information
serhiy-storchaka committed Aug 9, 2023
1 parent 7350738 commit 7b6e34e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Lib/re/_compiler.py
Expand Up @@ -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 _simple(av[2]):
emit(REPEATING_CODES[op][2])
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_re.py
Expand Up @@ -2342,6 +2342,16 @@ def test_bug_gh91616(self):
self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer
self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt"))

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])*)*'
Expand Down Expand Up @@ -2441,6 +2451,7 @@ def test_atomic_group(self):
17: SUCCESS
''')

@unittest.expectedFailure # gh-106052
def test_possesive_repeat_one(self):
self.assertEqual(get_debug_out(r'a?+'), '''\
POSSESSIVE_REPEAT 0 1
Expand All @@ -2453,6 +2464,7 @@ def test_possesive_repeat_one(self):
12: SUCCESS
''')

@unittest.expectedFailure # gh-106052
def test_possesive_repeat(self):
self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\
POSSESSIVE_REPEAT 0 1
Expand Down
@@ -0,0 +1,2 @@
:mod:`re` module: fix the matching of possessive quantifiers in the case of
a subpattern containing backtracking.

0 comments on commit 7b6e34e

Please sign in to comment.