Debian Codegraph @

rpython/rlib/rsre/test/test_ext_opcode.py

raw symbols SCIP
1 """
2 Test for cases that cannot be produced using the Python 2.7 sre_compile
3 module, but can be produced by other means (e.g. Python 3.5)
4 """
5
6 from rpython.rlib.rsre import rsre_core, rsre_constants
7 from rpython.rlib.rsre.rsre_char import MAXREPEAT
8 from rpython.rlib.rsre.test.support import match, Position
9
10 # import OPCODE_XX as XX
11 for name, value in rsre_constants.__dict__.items():
12 if name.startswith('OPCODE_') and isinstance(value, int):
13 globals()[name[7:]] = value
14
15 ab_plus_plus_bb = [POSSESSIVE_REPEAT, 7, 0, MAXREPEAT, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS, LITERAL, ord('b'), LITERAL, ord('b'), SUCCESS]
16
17 def test_repeat_one_with_backref():
18 # Python 3.5 compiles "(.)\1*" using REPEAT_ONE instead of REPEAT:
19 # it's a valid optimization because \1 is always one character long
20 r = [MARK, 0, ANY, MARK, 1, REPEAT_ONE, 6, 0, MAXREPEAT,
21 GROUPREF, 0, SUCCESS, SUCCESS]
22 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaa").match_end == 3
23
24 def test_min_repeat_one_with_backref():
25 # Python 3.5 compiles "(.)\1*?b" using MIN_REPEAT_ONE
26 r = [MARK, 0, ANY, MARK, 1, MIN_REPEAT_ONE, 6, 0, MAXREPEAT,
27 GROUPREF, 0, SUCCESS, LITERAL, 98, SUCCESS]
28 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaab").match_end == 4
29
30 def test_possessive_repeat_one():
31 r = [POSSESSIVE_REPEAT_ONE, 6, 0, MAXREPEAT, LITERAL, ord('a'), SUCCESS, LITERAL, ord('b'), SUCCESS]
32 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaab").match_end == 4
33 r = [POSSESSIVE_REPEAT_ONE, 6, 0, MAXREPEAT, LITERAL, ord('a'), SUCCESS, LITERAL, ord('a'), SUCCESS]
34 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "aaaa") is None
35
36 def test_possessive_repeat_one_min_count():
37 # x++ (min=1): match must fail when the starting position has zero
38 # repetitions, even though the tail (SUCCESS) would accept zero chars.
39 # Regression test for missing "if ptr < minptr: return" guard.
40 r = [POSSESSIVE_REPEAT_ONE, 5, 1, MAXREPEAT, LITERAL, ord('x'), SUCCESS, SUCCESS]
41 # 'axx' - position 0 has no 'x', so match at 0 must fail
42 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "axx") is None
43 # search must skip position 0 and find the run at position 1
44 ctx = rsre_core.search(rsre_core.CompiledPattern(r, 0), "axx")
45 assert ctx is not None
46 assert ctx.match_start == 1
47 assert ctx.match_end == 3
48 # 'xx' at position 0 - match must succeed consuming both chars
49 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xx").match_end == 2
50
51 def test_possessive_repeat():
52 r = ab_plus_plus_bb
53 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "abababababbb").match_end == 12
54 r = [POSSESSIVE_REPEAT, 7, 0, MAXREPEAT, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS]
55 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "abababababababab") is None
56 r = [POSSESSIVE_REPEAT, 7, 0, MAXREPEAT, LITERAL, ord('a'), LITERAL, ord('b'), SUCCESS, LITERAL, ord('c'), SUCCESS]
57 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ababababababababc") is not None
58 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ababababababababc", fullmatch=True) is not None
59
60 def test_atomic_group():
61 r = [ATOMIC_GROUP, 11, LITERAL, ord('a'), REPEAT_ONE, 6, 0, 1, LITERAL, ord('b'), SUCCESS, SUCCESS, LITERAL, ord('b'), SUCCESS]
62 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "abb").match_end == 3
63 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ab") is None
64
65 def test_atomic_group_fullmatch_bug():
66 r = [ATOMIC_GROUP, 4, LITERAL, ord('t'), SUCCESS, LITERAL, ord('a'), SUCCESS]
67 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ta") is not None
68 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "ta", fullmatch=True) is not None
69
70 def test_possessive_repeat_of_atomic_group():
71 # (?>x)++x
72 r = [POSSESSIVE_REPEAT, 8, 1, MAXREPEAT, ATOMIC_GROUP, 4, LITERAL, ord('x'), SUCCESS, SUCCESS, LITERAL, ord('x'), SUCCESS]
73 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xxx") is None
74 # '(?>x++)x'
75 r = [ATOMIC_GROUP, 9, POSSESSIVE_REPEAT, 6, 1, MAXREPEAT, LITERAL, ord('x'), SUCCESS, SUCCESS, LITERAL, ord('x'), SUCCESS]
76 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xxx") is None
77
78 def test_possessive_repeat_mark():
79 # '(.)++.'
80 r = [POSSESSIVE_REPEAT, 8, 1, MAXREPEAT, MARK, 0, ANY, MARK, 1, SUCCESS, ANY, SUCCESS]
81 assert rsre_core.match(rsre_core.CompiledPattern(r, 0), "xxx") is None
82
83 def test_possesive_repeat_groups():
84 # (.){3}+
85 r = [POSSESSIVE_REPEAT, 8, 3, 3, MARK, 0, ANY, MARK, 1, SUCCESS, SUCCESS]
86 match = rsre_core.match(rsre_core.CompiledPattern(r, 0), "abc")
87 assert match.match_marks is not None
88
89 def test_possessive_repeat_zero_width():
90 # (e?){2,4}+a
91 r = [POSSESSIVE_REPEAT, 14, 2, 4, MARK, 0, REPEAT_ONE, 6, 0, 1, LITERAL, ord('e'), SUCCESS, MARK, 1, SUCCESS, LITERAL, ord('a'), SUCCESS]
92 match = rsre_core.match(rsre_core.CompiledPattern(r, 0), "eeea")
93 assert match.match_marks is not None