Debian Codegraph @

rpython/rlib/rsre/test/test_zexternal.py

raw symbols SCIP
1 import re
2 from rpython.rlib.rsre.test.test_match import get_code
3 from rpython.rlib.rsre.test import support
4
5
6 def test_external_match():
7 from rpython.rlib.rsre.test.re_tests import tests
8 for t in tests:
9 yield run_external, t, False
10
11 def test_external_search():
12 from rpython.rlib.rsre.test.re_tests import tests
13 for t in tests:
14 yield run_external, t, True
15
16 def run_external(t, use_search):
17 from rpython.rlib.rsre.test.re_tests import SUCCEED, FAIL, SYNTAX_ERROR
18 pattern, s, outcome = t[:3]
19 if len(t) == 5:
20 repl, expected = t[3:5]
21 else:
22 assert len(t) == 3
23 print 'trying:', t
24 try:
25 obj = get_code(pattern)
26 except re.error:
27 if outcome == SYNTAX_ERROR:
28 return # Expected a syntax error
29 raise
30 if outcome == SYNTAX_ERROR:
31 raise Exception("this should have been a syntax error")
32 #
33 if use_search:
34 result = support.search(obj, s)
35 else:
36 # Emulate a poor man's search() with repeated match()s
37 for i in range(len(s)+1):
38 result = support.match(obj, s, start=i)
39 if result:
40 break
41 #
42 if outcome == FAIL:
43 if result is not None:
44 raise Exception("succeeded incorrectly")
45 elif outcome == SUCCEED:
46 if result is None:
47 raise Exception("failed incorrectly")
48 # Matched, as expected, so now we compute the
49 # result string and compare it to our expected result.
50 start, end = result.span(0)
51 vardict={'found': result.group(0),
52 'groups': result.group(),
53 }#'flags': result.re.flags}
54 for i in range(1, 100):
55 try:
56 gi = result.group(i)
57 # Special hack because else the string concat fails:
58 if gi is None:
59 gi = "None"
60 except IndexError:
61 gi = "Error"
62 vardict['g%d' % i] = gi
63 #for i in result.re.groupindex.keys():
64 # try:
65 # gi = result.group(i)
66 # if gi is None:
67 # gi = "None"
68 # except IndexError:
69 # gi = "Error"
70 # vardict[i] = gi
71 repl = eval(repl, vardict)
72 if repl != expected:
73 raise Exception("grouping error: %r should be %r" % (repl,
74 expected))