Skip to content

Commit 26137e2

Browse files
[3.11] gh-100061: Proper fix of the bug in the matching of possessive quantifiers (GH-102612) (GH-108004)
Restore the global Input Stream pointer after trying to match a sub-pattern. Co-authored-by: Ma Lin <[email protected]> (cherry picked from commit abd9cc5) Co-authored-by: SKO <[email protected]>
1 parent 4f35d4f commit 26137e2

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

Lib/re/_compiler.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ 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)
110103
elif op in REPEATING_CODES:
111104
if flags & SRE_FLAG_TEMPLATE:
112105
raise error("internal: unsupported template operator %r" % (op,))

Lib/test/test_re.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,16 @@ def test_template_function_and_flag_is_deprecated(self):
23972397
self.assertFalse(template_re1.match('nope'))
23982398

23992399
def test_bug_gh106052(self):
2400+
# gh-100061
2401+
self.assertEqual(re.match('(?>(?:.(?!D))+)', 'ABCDE').span(), (0, 2))
2402+
self.assertEqual(re.match('(?:.(?!D))++', 'ABCDE').span(), (0, 2))
2403+
self.assertEqual(re.match('(?>(?:.(?!D))*)', 'ABCDE').span(), (0, 2))
2404+
self.assertEqual(re.match('(?:.(?!D))*+', 'ABCDE').span(), (0, 2))
2405+
self.assertEqual(re.match('(?>(?:.(?!D))?)', 'CDE').span(), (0, 0))
2406+
self.assertEqual(re.match('(?:.(?!D))?+', 'CDE').span(), (0, 0))
2407+
self.assertEqual(re.match('(?>(?:.(?!D)){1,3})', 'ABCDE').span(), (0, 2))
2408+
self.assertEqual(re.match('(?:.(?!D)){1,3}+', 'ABCDE').span(), (0, 2))
2409+
# gh-106052
24002410
self.assertEqual(re.match("(?>(?:ab?c)+)", "aca").span(), (0, 2))
24012411
self.assertEqual(re.match("(?:ab?c)++", "aca").span(), (0, 2))
24022412
self.assertEqual(re.match("(?>(?:ab?c)*)", "aca").span(), (0, 2))
@@ -2502,7 +2512,6 @@ def test_atomic_group(self):
25022512
17: SUCCESS
25032513
''')
25042514

2505-
@unittest.expectedFailure # gh-106052
25062515
def test_possesive_repeat_one(self):
25072516
self.assertEqual(get_debug_out(r'a?+'), '''\
25082517
POSSESSIVE_REPEAT 0 1
@@ -2515,7 +2524,6 @@ def test_possesive_repeat_one(self):
25152524
12: SUCCESS
25162525
''')
25172526

2518-
@unittest.expectedFailure # gh-106052
25192527
def test_possesive_repeat(self):
25202528
self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\
25212529
POSSESSIVE_REPEAT 0 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug that causes wrong matches for regular expressions with possessive
2+
qualifier.

Modules/_sre/sre_lib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,10 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
13341334
MARK_POP(ctx->lastmark);
13351335
LASTMARK_RESTORE();
13361336

1337+
/* Restore the global Input Stream pointer
1338+
since it can change after jumps. */
1339+
state->ptr = ptr;
1340+
13371341
/* We have sufficient matches, so exit loop. */
13381342
break;
13391343
}

0 commit comments

Comments
 (0)