blob: e8791519d50c493c2d903163c64e0b0c45a889be [file] [log] [blame]
Fredrik Lundh143328b2000-09-02 11:03:34 +00001# SRE test harness for the Python regression suite
2
3# this is based on test_re.py, but uses a test function instead
4# of all those asserts
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +00005
6import sys
7sys.path=['.']+sys.path
8
Martin v. Löwis339d0f72001-08-17 18:39:25 +00009from test_support import verbose, TestFailed, have_unicode
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000010import sre
Fredrik Lundhf2989b22001-02-18 12:05:16 +000011import sys, os, string, traceback
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000012
Fredrik Lundh143328b2000-09-02 11:03:34 +000013#
14# test support
15
16def test(expression, result, exception=None):
17 try:
18 r = eval(expression)
19 except:
20 if exception:
21 if not isinstance(sys.exc_value, exception):
22 print expression, "FAILED"
23 # display name, not actual value
24 if exception is sre.error:
25 print "expected", "sre.error"
26 else:
27 print "expected", exception.__name__
28 print "got", sys.exc_type.__name__, str(sys.exc_value)
29 else:
30 print expression, "FAILED"
31 traceback.print_exc(file=sys.stdout)
32 else:
33 if exception:
34 print expression, "FAILED"
35 if exception is sre.error:
36 print "expected", "sre.error"
37 else:
38 print "expected", exception.__name__
39 print "got result", repr(r)
40 else:
41 if r != result:
42 print expression, "FAILED"
43 print "expected", repr(result)
44 print "got result", repr(r)
45
46if verbose:
47 print 'Running tests on character literals'
48
Fredrik Lundh510c97b2000-09-02 16:36:57 +000049for i in [0, 8, 16, 32, 64, 127, 128, 255]:
Fredrik Lundh538f05c2001-01-14 15:15:37 +000050 test(r"""sre.match(r"\%03o" % i, chr(i)) is not None""", 1)
51 test(r"""sre.match(r"\%03o0" % i, chr(i)+"0") is not None""", 1)
52 test(r"""sre.match(r"\%03o8" % i, chr(i)+"8") is not None""", 1)
53 test(r"""sre.match(r"\x%02x" % i, chr(i)) is not None""", 1)
54 test(r"""sre.match(r"\x%02x0" % i, chr(i)+"0") is not None""", 1)
55 test(r"""sre.match(r"\x%02xz" % i, chr(i)+"z") is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +000056test(r"""sre.match("\911", "")""", None, sre.error)
57
58#
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000059# Misc tests from Tim Peters' re.doc
60
61if verbose:
62 print 'Running tests on sre.search and sre.match'
63
Fredrik Lundh03dd0102000-09-03 10:43:16 +000064test(r"""sre.search(r'x*', 'axx').span(0)""", (0, 0))
65test(r"""sre.search(r'x*', 'axx').span()""", (0, 0))
66test(r"""sre.search(r'x+', 'axx').span(0)""", (1, 3))
67test(r"""sre.search(r'x+', 'axx').span()""", (1, 3))
68test(r"""sre.search(r'x', 'aaa')""", None)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000069
Fredrik Lundh03dd0102000-09-03 10:43:16 +000070test(r"""sre.match(r'a*', 'xxx').span(0)""", (0, 0))
71test(r"""sre.match(r'a*', 'xxx').span()""", (0, 0))
72test(r"""sre.match(r'x*', 'xxxa').span(0)""", (0, 3))
73test(r"""sre.match(r'x*', 'xxxa').span()""", (0, 3))
74test(r"""sre.match(r'a+', 'xxx')""", None)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000075
Fredrik Lundh510c97b2000-09-02 16:36:57 +000076# bug 113254
Fredrik Lundh03dd0102000-09-03 10:43:16 +000077test(r"""sre.match(r'(a)|(b)', 'b').start(1)""", -1)
78test(r"""sre.match(r'(a)|(b)', 'b').end(1)""", -1)
79test(r"""sre.match(r'(a)|(b)', 'b').span(1)""", (-1, -1))
Fredrik Lundh510c97b2000-09-02 16:36:57 +000080
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000081if verbose:
82 print 'Running tests on sre.sub'
83
Fredrik Lundh03dd0102000-09-03 10:43:16 +000084test(r"""sre.sub(r"(?i)b+", "x", "bbbb BBBB")""", 'x x')
Fredrik Lundh6f013982000-07-03 18:44:21 +000085
Fredrik Lundh143328b2000-09-02 11:03:34 +000086def bump_num(matchobj):
87 int_value = int(matchobj.group(0))
88 return str(int_value + 1)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000089
Fredrik Lundh143328b2000-09-02 11:03:34 +000090test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y')
91test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
Fredrik Lundh6f013982000-07-03 18:44:21 +000092
Fredrik Lundh03dd0102000-09-03 10:43:16 +000093test(r"""sre.sub(r'.', lambda m: r"\n", 'x')""", '\\n')
94test(r"""sre.sub(r'.', r"\n", 'x')""", '\n')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000095
Fredrik Lundh143328b2000-09-02 11:03:34 +000096s = r"\1\1"
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +000097
Fredrik Lundh03dd0102000-09-03 10:43:16 +000098test(r"""sre.sub(r'(.)', s, 'x')""", 'xx')
99test(r"""sre.sub(r'(.)', sre.escape(s), 'x')""", s)
100test(r"""sre.sub(r'(.)', lambda m: s, 'x')""", s)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000101
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000102test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
103test(r"""sre.sub(r'(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
104test(r"""sre.sub(r'(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
105test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000106
Fredrik Lundh59b68652001-09-18 20:55:24 +0000107# bug 449964: fails for group followed by other escape
108test(r"""sre.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx')""", 'xx\bxx\b')
109
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000110test(r"""sre.sub(r'a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D', 'a')""", '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
111test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
112test(r"""sre.sub(r'a', '\t\n\v\r\f\a', 'a')""", (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000113
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000114test(r"""sre.sub(r'^\s*', 'X', 'test')""", 'Xtest')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000115
Fredrik Lundh143328b2000-09-02 11:03:34 +0000116# qualified sub
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000117test(r"""sre.sub(r'a', 'b', 'aaaaa')""", 'bbbbb')
118test(r"""sre.sub(r'a', 'b', 'aaaaa', 1)""", 'baaaa')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000119
Fredrik Lundh19f977b2000-09-24 14:46:23 +0000120# bug 114660
121test(r"""sre.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there')""", 'hello there')
122
Guido van Rossume056e4d2001-08-10 14:52:48 +0000123# Test for sub() on escaped characters, see SF bug #449000
124test(r"""sre.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
125test(r"""sre.sub('\r\n', r'\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
126test(r"""sre.sub(r'\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
127test(r"""sre.sub('\r\n', '\n', 'abc\r\ndef\r\n')""", 'abc\ndef\n')
128
Fredrik Lundh21009b92001-09-18 18:47:09 +0000129# Test for empty sub() behaviour, see SF bug #462270
130test(r"""sre.sub('x*', '-', 'abxd')""", '-a-b-d-')
131test(r"""sre.sub('x+', '-', 'abxd')""", 'ab-d')
132
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000133if verbose:
134 print 'Running tests on symbolic references'
135
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000136test(r"""sre.sub(r'(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
137test(r"""sre.sub(r'(?P<a>x)', '\g<', 'xx')""", None, sre.error)
138test(r"""sre.sub(r'(?P<a>x)', '\g', 'xx')""", None, sre.error)
139test(r"""sre.sub(r'(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
140test(r"""sre.sub(r'(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
141test(r"""sre.sub(r'(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
142test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
143test(r"""sre.sub(r'(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000144
145if verbose:
146 print 'Running tests on sre.subn'
147
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000148test(r"""sre.subn(r"(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
149test(r"""sre.subn(r"b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
150test(r"""sre.subn(r"b+", "x", "xyz")""", ('xyz', 0))
151test(r"""sre.subn(r"b*", "x", "xyz")""", ('xxxyxzx', 4))
152test(r"""sre.subn(r"b*", "x", "xyz", 2)""", ('xxxyz', 2))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000153
154if verbose:
155 print 'Running tests on sre.split'
Fredrik Lundh6f013982000-07-03 18:44:21 +0000156
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000157test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
158test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
159test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
160test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
161test(r"""sre.split(r"(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
162test(r"""sre.split(r"([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
163test(r"""sre.split(r"(b)|(:+)", ":a:b::c")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000164 ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000165test(r"""sre.split(r"(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000166
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000167test(r"""sre.split(r":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
168test(r"""sre.split(r':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000169
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000170test(r"""sre.split(r"(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
171test(r"""sre.split(r"(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000172
173if verbose:
174 print "Running tests on sre.findall"
175
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000176test(r"""sre.findall(r":+", "abc")""", [])
177test(r"""sre.findall(r":+", "a:b::c:::d")""", [":", "::", ":::"])
178test(r"""sre.findall(r"(:+)", "a:b::c:::d")""", [":", "::", ":::"])
179test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""",
Fredrik Lundh143328b2000-09-02 11:03:34 +0000180 [(":", ""), (":", ":"), (":", "::")])
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000181test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")])
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000182
Fredrik Lundhebc37b22000-10-28 19:30:41 +0000183# bug 117612
184test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")])
185
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000186if verbose:
187 print "Running tests on sre.match"
188
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000189test(r"""sre.match(r'a', 'a').groups()""", ())
190test(r"""sre.match(r'(a)', 'a').groups()""", ('a',))
191test(r"""sre.match(r'(a)', 'a').group(0)""", 'a')
192test(r"""sre.match(r'(a)', 'a').group(1)""", 'a')
193test(r"""sre.match(r'(a)', 'a').group(1, 1)""", ('a', 'a'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000194
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000195pat = sre.compile(r'((a)|(b))(c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000196test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
197test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
198test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
199test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
200test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000201
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000202pat = sre.compile(r'(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
Fredrik Lundh143328b2000-09-02 11:03:34 +0000203test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
204test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
205test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000206
Fredrik Lundh397a6542001-10-18 19:30:16 +0000207# bug 448951 (similar to 429357, but with single char match)
208# (Also test greedy matches.)
209for op in '','?','*':
210 test(r"""sre.match(r'((.%s):)?z', 'z').groups()"""%op, (None, None))
211 test(r"""sre.match(r'((.%s):)?z', 'a:z').groups()"""%op, ('a:', 'a'))
212
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000213if verbose:
214 print "Running tests on sre.escape"
215
Fredrik Lundh143328b2000-09-02 11:03:34 +0000216p = ""
217for i in range(0, 256):
218 p = p + chr(i)
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000219 test(r"""sre.match(sre.escape(chr(i)), chr(i)) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000220 test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000221
Fredrik Lundh143328b2000-09-02 11:03:34 +0000222pat = sre.compile(sre.escape(p))
Fredrik Lundh538f05c2001-01-14 15:15:37 +0000223test(r"""pat.match(p) is not None""", 1)
Fredrik Lundh143328b2000-09-02 11:03:34 +0000224test(r"""pat.match(p).span()""", (0,256))
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000225
226if verbose:
Fredrik Lundh1296a8d2001-10-21 18:04:11 +0000227 print 'Running tests on sre.Scanner'
228
229def s_ident(scanner, token): return token
230def s_operator(scanner, token): return "op%s" % token
231def s_float(scanner, token): return float(token)
232def s_int(scanner, token): return int(token)
233
234scanner = sre.Scanner([
235 (r"[a-zA-Z_]\w*", s_ident),
236 (r"\d+\.\d*", s_float),
237 (r"\d+", s_int),
238 (r"=|\+|-|\*|/", s_operator),
239 (r"\s+", None),
240 ])
241
242# sanity check
243test('scanner.scan("sum = 3*foo + 312.50 + bar")',
244 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5, 'op+', 'bar'], ''))
245
246if verbose:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000247 print 'Pickling a SRE_Pattern instance'
248
249try:
250 import pickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000251 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000252 s = pickle.dumps(pat)
253 pat = pickle.loads(s)
254except:
255 print TestFailed, 're module pickle' # expected
256
257try:
258 import cPickle
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000259 pat = sre.compile(r'a(?:b|(c|e){1,2}?|d)+?(.)')
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000260 s = cPickle.dumps(pat)
261 pat = cPickle.loads(s)
262except:
263 print TestFailed, 're module cPickle' # expected
264
Fredrik Lundh143328b2000-09-02 11:03:34 +0000265# constants
266test(r"""sre.I""", sre.IGNORECASE)
267test(r"""sre.L""", sre.LOCALE)
268test(r"""sre.M""", sre.MULTILINE)
269test(r"""sre.S""", sre.DOTALL)
270test(r"""sre.X""", sre.VERBOSE)
271test(r"""sre.T""", sre.TEMPLATE)
272test(r"""sre.U""", sre.UNICODE)
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000273
274for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
275 try:
276 r = sre.compile('^pattern$', flags)
277 except:
278 print 'Exception raised on flag', flags
279
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000280if verbose:
281 print 'Test engine limitations'
282
283# Try nasty case that overflows the straightforward recursive
284# implementation of repeated groups.
Fredrik Lundh015415e2001-03-22 23:48:28 +0000285test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError)
286test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError)
Fredrik Lundhdf781e62001-07-02 19:54:28 +0000287test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001))
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000288
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000289from re_tests import *
290
291if verbose:
292 print 'Running re_tests test suite'
293else:
294 # To save time, only run the first and last 10 tests
295 #tests = tests[:10] + tests[-10:]
Fredrik Lundh6f013982000-07-03 18:44:21 +0000296 pass
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000297
298for t in tests:
299 sys.stdout.flush()
300 pattern=s=outcome=repl=expected=None
301 if len(t)==5:
302 pattern, s, outcome, repl, expected = t
303 elif len(t)==3:
Fredrik Lundh6f013982000-07-03 18:44:21 +0000304 pattern, s, outcome = t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000305 else:
306 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
307
308 try:
309 obj=sre.compile(pattern)
310 except sre.error:
311 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
Fredrik Lundh6f013982000-07-03 18:44:21 +0000312 else:
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000313 print '=== Syntax error:', t
314 except KeyboardInterrupt: raise KeyboardInterrupt
315 except:
316 print '*** Unexpected error ***', t
317 if verbose:
318 traceback.print_exc(file=sys.stdout)
319 else:
320 try:
321 result=obj.search(s)
322 except (sre.error), msg:
323 print '=== Unexpected exception', t, repr(msg)
324 if outcome==SYNTAX_ERROR:
Fredrik Lundh03dd0102000-09-03 10:43:16 +0000325 print '=== Compiled incorrectly', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000326 elif outcome==FAIL:
327 if result is None: pass # No match, as expected
328 else: print '=== Succeeded incorrectly', t
329 elif outcome==SUCCEED:
330 if result is not None:
331 # Matched, as expected, so now we compute the
332 # result string and compare it to our expected result.
333 start, end = result.span(0)
334 vardict={'found': result.group(0),
335 'groups': result.group(),
336 'flags': result.re.flags}
337 for i in range(1, 100):
338 try:
339 gi = result.group(i)
340 # Special hack because else the string concat fails:
341 if gi is None:
342 gi = "None"
343 except IndexError:
344 gi = "Error"
345 vardict['g%d' % i] = gi
346 for i in result.re.groupindex.keys():
347 try:
348 gi = result.group(i)
349 if gi is None:
350 gi = "None"
351 except IndexError:
352 gi = "Error"
353 vardict[i] = gi
354 repl=eval(repl, vardict)
355 if repl!=expected:
356 print '=== grouping error', t,
357 print repr(repl)+' should be '+repr(expected)
358 else:
359 print '=== Failed incorrectly', t
Fredrik Lundh90a07912000-06-30 07:50:59 +0000360 continue
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000361
362 # Try the match on a unicode string, and check that it
363 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000364 try:
365 u = unicode(s, "latin-1")
366 except NameError:
367 pass
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000368 except TypeError:
369 continue # skip unicode test strings
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000370 else:
371 result=obj.search(u)
372 if result==None:
373 print '=== Fails on unicode match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000374
375 # Try the match on a unicode pattern, and check that it
376 # still succeeds.
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000377 try:
378 u = unicode(pattern, "latin-1")
379 except NameError:
380 pass
381 else:
382 obj=sre.compile(u)
383 result=obj.search(s)
384 if result==None:
385 print '=== Fails on unicode pattern match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000386
387 # Try the match with the search area limited to the extent
388 # of the match and see if it still succeeds. \B will
389 # break (because it won't match at the end or start of a
390 # string), so we'll ignore patterns that feature it.
Fredrik Lundh6f013982000-07-03 18:44:21 +0000391
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000392 if pattern[:2]!='\\B' and pattern[-2:]!='\\B':
393 obj=sre.compile(pattern)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000394 result=obj.search(s, result.start(0), result.end(0)+1)
395 if result==None:
396 print '=== Failed on range-limited match', t
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +0000397
398 # Try the match with IGNORECASE enabled, and check that it
399 # still succeeds.
400 obj=sre.compile(pattern, sre.IGNORECASE)
401 result=obj.search(s)
402 if result==None:
403 print '=== Fails on case-insensitive match', t
404
405 # Try the match with LOCALE enabled, and check that it
406 # still succeeds.
407 obj=sre.compile(pattern, sre.LOCALE)
408 result=obj.search(s)
409 if result==None:
410 print '=== Fails on locale-sensitive match', t
411
Fredrik Lundhc2ed6212000-08-01 13:01:43 +0000412 # Try the match with UNICODE locale enabled, and check
413 # that it still succeeds.
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000414 if have_unicode:
415 obj=sre.compile(pattern, sre.UNICODE)
416 result=obj.search(s)
417 if result==None:
418 print '=== Fails on unicode-sensitive match', t