Fix re.fullmatch POSSESSIVE_REPEAT (#7187)

This commit is contained in:
wvmscs authored and GitHub committed 2026-02-20 09:51:34 +09:00
1 parent eddb8f271a
commit dfc5de701b
4 files changed
+26 -6

No files matched your search

-3
View File
@@ -2537,7 +2537,6 @@ class ReTests(unittest.TestCase):
self.assertIsNone(re.match("^x{}+$", "xxx"))
self.assertTrue(re.match("^x{}+$", "x{}"))
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_fullmatch_possessive_quantifiers(self):
self.assertTrue(re.fullmatch(r'a++', 'a'))
self.assertTrue(re.fullmatch(r'a*+', 'a'))
@@ -2590,7 +2589,6 @@ class ReTests(unittest.TestCase):
self.assertIsNone(re.match(r'(?>x)++x', 'xxx'))
self.assertIsNone(re.match(r'(?>x++)x', 'xxx'))
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_fullmatch_atomic_grouping(self):
self.assertTrue(re.fullmatch(r'(?>a+)', 'a'))
self.assertTrue(re.fullmatch(r'(?>a*)', 'a'))
@@ -2629,7 +2627,6 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.findall(r'(?>(?:ab)?)', 'ababc'), ['ab', 'ab', '', ''])
self.assertEqual(re.findall(r'(?>(?:ab){1,3})', 'ababc'), ['abab'])
@unittest.expectedFailure # TODO: RUSTPYTHON
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"))
+10 -3
View File
@@ -470,7 +470,10 @@ fn _match<S: StrDrive>(req: &Request<'_, S>, state: &mut State, mut ctx: MatchCo
Jump::PossessiveRepeat1 => {
let min_count = ctx.peek_code(req, 2) as isize;
if ctx.count < min_count {
break 'context ctx.next_offset(4, Jump::PossessiveRepeat2);
// modified next.toplevel from inherited to false
let mut next = ctx.next_offset(4, Jump::PossessiveRepeat2);
next.toplevel = false;
break 'context next;
}
// zero match protection
ctx.cursor.position = usize::MAX;
@@ -494,7 +497,9 @@ fn _match<S: StrDrive>(req: &Request<'_, S>, state: &mut State, mut ctx: MatchCo
{
state.marks.push();
ctx.cursor = state.cursor;
break 'context ctx.next_offset(4, Jump::PossessiveRepeat4);
let mut next = ctx.next_offset(4, Jump::PossessiveRepeat4);
next.toplevel = false; // modified next.toplevel from inherited to false
break 'context next;
}
ctx.cursor = state.cursor;
ctx.skip_code_from(req, 1);
@@ -832,7 +837,9 @@ fn _match<S: StrDrive>(req: &Request<'_, S>, state: &mut State, mut ctx: MatchCo
/* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
SreOpcode::ATOMIC_GROUP => {
state.cursor = ctx.cursor;
break 'context ctx.next_offset(2, Jump::AtomicGroup1);
let mut next_ctx = ctx.next_offset(2, Jump::AtomicGroup1);
next_ctx.toplevel = false; // modified next.toplevel from inherited to false
break 'context next_ctx;
}
/* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
<SUCCESS> tail */
+12
View File
@@ -145,6 +145,18 @@ fn test_possessive_quantifier() {
assert!(state.py_match(&req));
}
#[test]
fn test_possessive_repeat_fullmatch() {
// pattern p = re.compile("([0-9]++(?:\.[0-9]+)*+)", re.I )
// [INFO, 4, 0, 1, 4294967295, MARK, 0, POSSESSIVE_REPEAT_ONE, 10, 1, MAXREPEAT, IN, 5, RANGE, 48, 57, FAILURE, SUCCESS, POSSESSIVE_REPEAT, 16, 0, MAXREPEAT, LITERAL, 46, REPEAT_ONE, 10, 1, MAXREPEAT, IN, 5, RANGE, 48, 57, FAILURE, SUCCESS, SUCCESS, MARK, 1, SUCCESS]
// START GENERATED by generate_tests.py
#[rustfmt::skip] let p = Pattern { pattern: "([0-9]++(?:\\.[0-9]+)*+)", code: &[14, 4, 0, 1, 4294967295, 17, 0, 29, 10, 1, 4294967295, 13, 5, 22, 48, 57, 0, 1, 28, 16, 0, 4294967295, 16, 46, 24, 10, 1, 4294967295, 13, 5, 22, 48, 57, 0, 1, 1, 17, 1, 1] };
// END GENERATED
let (mut req, mut state) = p.state("1.25.38");
req.match_all = true;
assert!(state.py_match(&req), "should match");
}
#[test]
fn test_possessive_atomic_group() {
// pattern p = re.compile('(?>x)++x')
+4
View File
@@ -75,3 +75,7 @@ assert urlpattern.match(url).group(1) == "www.example.org:80"
assert re.compile("(?:\w+(?:\s|/(?!>))*)*").match("a /bb />ccc").group() == "a /bb "
assert re.compile("(?:(1)?)*").match("111").group() == "111"
# Test of fix re.fullmatch POSSESSIVE_REPEAT, issue #7183
assert re.fullmatch(r"([0-9]++(?:\.[0-9]+)*+)", "1.25.38")
assert re.fullmatch(r"([0-9]++(?:\.[0-9]+)*+)", "1.25.38").group(0) == "1.25.38"