Skip to content

Commit

Permalink
[3.11] gh-100061: Proper fix of the bug in the matching of possessive…
Browse files Browse the repository at this point in the history
… quantifiers (GH-102612) (GH-108004)

Restore the global Input Stream pointer after trying to match a sub-pattern.

Co-authored-by: Ma Lin <animalize@users.noreply.github.com>

(cherry picked from commit abd9cc5)
    
Co-authored-by: SKO <41810398+uyw4687@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 16, 2023
1 parent 4f35d4f commit 26137e2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
7 changes: 0 additions & 7 deletions Lib/re/_compiler.py
Expand Up @@ -100,13 +100,6 @@ 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,))
Expand Down
12 changes: 10 additions & 2 deletions Lib/test/test_re.py
Expand Up @@ -2397,6 +2397,16 @@ def test_template_function_and_flag_is_deprecated(self):
self.assertFalse(template_re1.match('nope'))

def test_bug_gh106052(self):
# gh-100061
self.assertEqual(re.match('(?>(?:.(?!D))+)', 'ABCDE').span(), (0, 2))
self.assertEqual(re.match('(?:.(?!D))++', 'ABCDE').span(), (0, 2))
self.assertEqual(re.match('(?>(?:.(?!D))*)', 'ABCDE').span(), (0, 2))
self.assertEqual(re.match('(?:.(?!D))*+', 'ABCDE').span(), (0, 2))
self.assertEqual(re.match('(?>(?:.(?!D))?)', 'CDE').span(), (0, 0))
self.assertEqual(re.match('(?:.(?!D))?+', 'CDE').span(), (0, 0))
self.assertEqual(re.match('(?>(?:.(?!D)){1,3})', 'ABCDE').span(), (0, 2))
self.assertEqual(re.match('(?:.(?!D)){1,3}+', 'ABCDE').span(), (0, 2))
# gh-106052
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))
Expand Down Expand Up @@ -2502,7 +2512,6 @@ 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 @@ -2515,7 +2524,6 @@ 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 @@
Fix a bug that causes wrong matches for regular expressions with possessive
qualifier.
4 changes: 4 additions & 0 deletions Modules/_sre/sre_lib.h
Expand Up @@ -1334,6 +1334,10 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
MARK_POP(ctx->lastmark);
LASTMARK_RESTORE();

/* Restore the global Input Stream pointer
since it can change after jumps. */
state->ptr = ptr;

/* We have sufficient matches, so exit loop. */
break;
}
Expand Down

0 comments on commit 26137e2

Please sign in to comment.