blob: 755cb00ae452c0f2b23f504db43a3dcdd750ad18 [file] [log] [blame]
Guido van Rossum8430c581998-04-03 21:47:12 +00001import sys
Fred Drake8ae9ce52000-08-18 16:09:56 +00002sys.path = ['.'] + sys.path
Guido van Rossum8430c581998-04-03 21:47:12 +00003
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import verbose, run_unittest, catch_warning
Guido van Rossum8e0ce301997-07-11 19:34:44 +00005import re
Thomas Wouters9ada3d62006-04-21 09:47:09 +00006from re import Scanner
Eric S. Raymond2846b0a2001-02-09 12:00:47 +00007import sys, os, traceback
Raymond Hettinger027bb632004-05-31 03:09:25 +00008from weakref import proxy
Guido van Rossum8e0ce301997-07-11 19:34:44 +00009
Guido van Rossum23b22571997-07-17 22:36:14 +000010# Misc tests from Tim Peters' re.doc
11
Just van Rossum6802c6e2003-07-02 14:36:59 +000012# WARNING: Don't change details in these tests if you don't know
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +000013# what you're doing. Some of these tests were carefuly modeled to
14# cover most of the code.
15
Skip Montanaro8ed06da2003-04-24 19:43:18 +000016import unittest
Guido van Rossum8430c581998-04-03 21:47:12 +000017
Skip Montanaro8ed06da2003-04-24 19:43:18 +000018class ReTests(unittest.TestCase):
Raymond Hettinger027bb632004-05-31 03:09:25 +000019
20 def test_weakref(self):
21 s = 'QabbbcR'
22 x = re.compile('ab+c')
23 y = proxy(x)
24 self.assertEqual(x.findall('QabbbcR'), y.findall('QabbbcR'))
25
Skip Montanaro8ed06da2003-04-24 19:43:18 +000026 def test_search_star_plus(self):
27 self.assertEqual(re.search('x*', 'axx').span(0), (0, 0))
28 self.assertEqual(re.search('x*', 'axx').span(), (0, 0))
29 self.assertEqual(re.search('x+', 'axx').span(0), (1, 3))
30 self.assertEqual(re.search('x+', 'axx').span(), (1, 3))
Skip Montanaro5ba00542003-04-25 16:00:14 +000031 self.assertEqual(re.search('x', 'aaa'), None)
Skip Montanaro8ed06da2003-04-24 19:43:18 +000032 self.assertEqual(re.match('a*', 'xxx').span(0), (0, 0))
33 self.assertEqual(re.match('a*', 'xxx').span(), (0, 0))
34 self.assertEqual(re.match('x*', 'xxxa').span(0), (0, 3))
35 self.assertEqual(re.match('x*', 'xxxa').span(), (0, 3))
Skip Montanaro5ba00542003-04-25 16:00:14 +000036 self.assertEqual(re.match('a+', 'xxx'), None)
Guido van Rossum8430c581998-04-03 21:47:12 +000037
Skip Montanaro8ed06da2003-04-24 19:43:18 +000038 def bump_num(self, matchobj):
Guido van Rossum41360a41998-03-26 19:42:58 +000039 int_value = int(matchobj.group(0))
40 return str(int_value + 1)
Guido van Rossum23b22571997-07-17 22:36:14 +000041
Skip Montanaro8ed06da2003-04-24 19:43:18 +000042 def test_basic_re_sub(self):
43 self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
44 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
45 '9.3 -3 24x100y')
46 self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3),
47 '9.3 -3 23x99y')
Fredrik Lundh1151a8c2000-08-08 16:47:42 +000048
Skip Montanaro8ed06da2003-04-24 19:43:18 +000049 self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
50 self.assertEqual(re.sub('.', r"\n", 'x'), '\n')
Guido van Rossumdfa67901997-12-08 17:12:06 +000051
Skip Montanaro8ed06da2003-04-24 19:43:18 +000052 s = r"\1\1"
53 self.assertEqual(re.sub('(.)', s, 'x'), 'xx')
54 self.assertEqual(re.sub('(.)', re.escape(s), 'x'), s)
55 self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)
Guido van Rossum23b22571997-07-17 22:36:14 +000056
Skip Montanaro8ed06da2003-04-24 19:43:18 +000057 self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx'), 'xxxx')
58 self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx'), 'xxxx')
59 self.assertEqual(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx'), 'xxxx')
60 self.assertEqual(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx'), 'xxxx')
Guido van Rossum49946571997-07-18 04:26:25 +000061
Skip Montanaro8ed06da2003-04-24 19:43:18 +000062 self.assertEqual(re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a'),
63 '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
64 self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'), '\t\n\v\r\f\a')
65 self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'),
66 (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
Guido van Rossum95e80531997-08-13 22:34:14 +000067
Skip Montanaro8ed06da2003-04-24 19:43:18 +000068 self.assertEqual(re.sub('^\s*', 'X', 'test'), 'Xtest')
Guido van Rossume056e4d2001-08-10 14:52:48 +000069
Skip Montanaro2726fcd2003-04-25 14:31:54 +000070 def test_bug_449964(self):
71 # fails for group followed by other escape
72 self.assertEqual(re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx'),
73 'xx\bxx\b')
74
75 def test_bug_449000(self):
76 # Test for sub() on escaped characters
Skip Montanaro8ed06da2003-04-24 19:43:18 +000077 self.assertEqual(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n'),
78 'abc\ndef\n')
79 self.assertEqual(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n'),
80 'abc\ndef\n')
81 self.assertEqual(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n'),
82 'abc\ndef\n')
83 self.assertEqual(re.sub('\r\n', '\n', 'abc\r\ndef\r\n'),
84 'abc\ndef\n')
Guido van Rossum23b22571997-07-17 22:36:14 +000085
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000086 def test_bug_1661(self):
87 # Verify that flags do not get silently ignored with compiled patterns
88 pattern = re.compile('.')
89 self.assertRaises(ValueError, re.match, pattern, 'A', re.I)
90 self.assertRaises(ValueError, re.search, pattern, 'A', re.I)
91 self.assertRaises(ValueError, re.findall, pattern, 'A', re.I)
92 self.assertRaises(ValueError, re.compile, pattern, re.I)
93
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +000094 def test_sub_template_numeric_escape(self):
95 # bug 776311 and friends
96 self.assertEqual(re.sub('x', r'\0', 'x'), '\0')
97 self.assertEqual(re.sub('x', r'\000', 'x'), '\000')
98 self.assertEqual(re.sub('x', r'\001', 'x'), '\001')
99 self.assertEqual(re.sub('x', r'\008', 'x'), '\0' + '8')
100 self.assertEqual(re.sub('x', r'\009', 'x'), '\0' + '9')
101 self.assertEqual(re.sub('x', r'\111', 'x'), '\111')
102 self.assertEqual(re.sub('x', r'\117', 'x'), '\117')
103
104 self.assertEqual(re.sub('x', r'\1111', 'x'), '\1111')
105 self.assertEqual(re.sub('x', r'\1111', 'x'), '\111' + '1')
106
107 self.assertEqual(re.sub('x', r'\00', 'x'), '\x00')
108 self.assertEqual(re.sub('x', r'\07', 'x'), '\x07')
109 self.assertEqual(re.sub('x', r'\08', 'x'), '\0' + '8')
110 self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9')
111 self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a')
112
113 self.assertEqual(re.sub('x', r'\400', 'x'), '\0')
114 self.assertEqual(re.sub('x', r'\777', 'x'), '\377')
Tim Peters0e9980f2004-09-12 03:49:31 +0000115
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000116 self.assertRaises(re.error, re.sub, 'x', r'\1', 'x')
117 self.assertRaises(re.error, re.sub, 'x', r'\8', 'x')
118 self.assertRaises(re.error, re.sub, 'x', r'\9', 'x')
119 self.assertRaises(re.error, re.sub, 'x', r'\11', 'x')
120 self.assertRaises(re.error, re.sub, 'x', r'\18', 'x')
121 self.assertRaises(re.error, re.sub, 'x', r'\1a', 'x')
122 self.assertRaises(re.error, re.sub, 'x', r'\90', 'x')
123 self.assertRaises(re.error, re.sub, 'x', r'\99', 'x')
124 self.assertRaises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8'
125 self.assertRaises(re.error, re.sub, 'x', r'\11a', 'x')
126 self.assertRaises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1'
127 self.assertRaises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0'
128
129 # in python2.3 (etc), these loop endlessly in sre_parser.py
130 self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x')
131 self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz'),
132 'xz8')
133 self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz'),
134 'xza')
135
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000136 def test_qualified_re_sub(self):
137 self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb')
138 self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa')
Guido van Rossum8430c581998-04-03 21:47:12 +0000139
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000140 def test_bug_114660(self):
141 self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there'),
142 'hello there')
143
144 def test_bug_462270(self):
145 # Test for empty sub() behaviour, see SF bug #462270
146 self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-')
147 self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d')
148
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000149 def test_symbolic_refs(self):
150 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
151 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
152 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
153 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
154 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
155 self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
156 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
157 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx')
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000158 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx')
Guido van Rossumf473cb01998-01-14 16:42:17 +0000159
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000160 def test_re_subn(self):
161 self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
162 self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1))
163 self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0))
164 self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4))
165 self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2))
Guido van Rossum49946571997-07-18 04:26:25 +0000166
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000167 def test_re_split(self):
168 self.assertEqual(re.split(":", ":a:b::c"), ['', 'a', 'b', '', 'c'])
169 self.assertEqual(re.split(":*", ":a:b::c"), ['', 'a', 'b', 'c'])
170 self.assertEqual(re.split("(:*)", ":a:b::c"),
171 ['', ':', 'a', ':', 'b', '::', 'c'])
172 self.assertEqual(re.split("(?::*)", ":a:b::c"), ['', 'a', 'b', 'c'])
173 self.assertEqual(re.split("(:)*", ":a:b::c"),
174 ['', ':', 'a', ':', 'b', ':', 'c'])
175 self.assertEqual(re.split("([b:]+)", ":a:b::c"),
176 ['', ':', 'a', ':b::', 'c'])
177 self.assertEqual(re.split("(b)|(:+)", ":a:b::c"),
178 ['', None, ':', 'a', None, ':', '', 'b', None, '',
179 None, '::', 'c'])
180 self.assertEqual(re.split("(?:b)|(?::+)", ":a:b::c"),
181 ['', 'a', '', '', 'c'])
Guido van Rossum49946571997-07-18 04:26:25 +0000182
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000183 def test_qualified_re_split(self):
184 self.assertEqual(re.split(":", ":a:b::c", 2), ['', 'a', 'b::c'])
185 self.assertEqual(re.split(':', 'a:b:c:d', 2), ['a', 'b', 'c:d'])
186 self.assertEqual(re.split("(:)", ":a:b::c", 2),
187 ['', ':', 'a', ':', 'b::c'])
188 self.assertEqual(re.split("(:*)", ":a:b::c", 2),
189 ['', ':', 'a', ':', 'b::c'])
Guido van Rossum49946571997-07-18 04:26:25 +0000190
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000191 def test_re_findall(self):
192 self.assertEqual(re.findall(":+", "abc"), [])
193 self.assertEqual(re.findall(":+", "a:b::c:::d"), [":", "::", ":::"])
194 self.assertEqual(re.findall("(:+)", "a:b::c:::d"), [":", "::", ":::"])
195 self.assertEqual(re.findall("(:)(:*)", "a:b::c:::d"), [(":", ""),
196 (":", ":"),
197 (":", "::")])
Guido van Rossum49946571997-07-18 04:26:25 +0000198
Skip Montanaro5ba00542003-04-25 16:00:14 +0000199 def test_bug_117612(self):
200 self.assertEqual(re.findall(r"(a|(b))", "aba"),
201 [("a", ""),("b", "b"),("a", "")])
202
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000203 def test_re_match(self):
Skip Montanaro5ba00542003-04-25 16:00:14 +0000204 self.assertEqual(re.match('a', 'a').groups(), ())
205 self.assertEqual(re.match('(a)', 'a').groups(), ('a',))
206 self.assertEqual(re.match(r'(a)', 'a').group(0), 'a')
207 self.assertEqual(re.match(r'(a)', 'a').group(1), 'a')
208 self.assertEqual(re.match(r'(a)', 'a').group(1, 1), ('a', 'a'))
Guido van Rossum49946571997-07-18 04:26:25 +0000209
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000210 pat = re.compile('((a)|(b))(c)?')
211 self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None))
212 self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None))
213 self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c'))
214 self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
215 self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
Guido van Rossum8430c581998-04-03 21:47:12 +0000216
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000217 # A single group
218 m = re.match('(a)', 'a')
219 self.assertEqual(m.group(0), 'a')
220 self.assertEqual(m.group(0), 'a')
221 self.assertEqual(m.group(1), 'a')
222 self.assertEqual(m.group(1, 1), ('a', 'a'))
Guido van Rossum49946571997-07-18 04:26:25 +0000223
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000224 pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
225 self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
226 self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
227 (None, 'b', None))
228 self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
Guido van Rossum49946571997-07-18 04:26:25 +0000229
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000230 def test_re_groupref_exists(self):
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000231 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
232 ('(', 'a'))
233 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
234 (None, 'a'))
235 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None)
236 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None)
237 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
238 ('a', 'b'))
239 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
240 (None, 'd'))
241 self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
242 (None, 'd'))
243 self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(),
244 ('a', ''))
245
Michael W. Hudsone7fa1af2005-06-03 13:55:58 +0000246 # Tests for bug #1177831: exercise groups other than the first group
247 p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
248 self.assertEqual(p.match('abc').groups(),
249 ('a', 'b', 'c'))
250 self.assertEqual(p.match('ad').groups(),
251 ('a', None, 'd'))
252 self.assertEqual(p.match('abd'), None)
253 self.assertEqual(p.match('ac'), None)
254
Andrew M. Kuchling3554cad2005-06-02 13:38:45 +0000255
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000256 def test_re_groupref(self):
257 self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
258 ('|', 'a'))
259 self.assertEqual(re.match(r'^(\|)?([^()]+)\1?$', 'a').groups(),
260 (None, 'a'))
261 self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', 'a|'), None)
262 self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a'), None)
263 self.assertEqual(re.match(r'^(?:(a)|c)(\1)$', 'aa').groups(),
264 ('a', 'a'))
265 self.assertEqual(re.match(r'^(?:(a)|c)(\1)?$', 'c').groups(),
266 (None, None))
267
268 def test_groupdict(self):
269 self.assertEqual(re.match('(?P<first>first) (?P<second>second)',
270 'first second').groupdict(),
271 {'first':'first', 'second':'second'})
272
273 def test_expand(self):
274 self.assertEqual(re.match("(?P<first>first) (?P<second>second)",
275 "first second")
276 .expand(r"\2 \1 \g<second> \g<first>"),
277 "second first second first")
278
279 def test_repeat_minmax(self):
280 self.assertEqual(re.match("^(\w){1}$", "abc"), None)
281 self.assertEqual(re.match("^(\w){1}?$", "abc"), None)
282 self.assertEqual(re.match("^(\w){1,2}$", "abc"), None)
283 self.assertEqual(re.match("^(\w){1,2}?$", "abc"), None)
284
285 self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
286 self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
287 self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
288 self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
289 self.assertEqual(re.match("^(\w){3}?$", "abc").group(1), "c")
290 self.assertEqual(re.match("^(\w){1,3}?$", "abc").group(1), "c")
291 self.assertEqual(re.match("^(\w){1,4}?$", "abc").group(1), "c")
292 self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
293
294 self.assertEqual(re.match("^x{1}$", "xxx"), None)
295 self.assertEqual(re.match("^x{1}?$", "xxx"), None)
296 self.assertEqual(re.match("^x{1,2}$", "xxx"), None)
297 self.assertEqual(re.match("^x{1,2}?$", "xxx"), None)
298
299 self.assertNotEqual(re.match("^x{3}$", "xxx"), None)
300 self.assertNotEqual(re.match("^x{1,3}$", "xxx"), None)
301 self.assertNotEqual(re.match("^x{1,4}$", "xxx"), None)
302 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)
303 self.assertNotEqual(re.match("^x{3}?$", "xxx"), None)
304 self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None)
305 self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None)
306 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)
307
Gustavo Niemeyer6fa0c5a2005-09-14 08:54:39 +0000308 self.assertEqual(re.match("^x{}$", "xxx"), None)
309 self.assertNotEqual(re.match("^x{}$", "x{}"), None)
310
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000311 def test_getattr(self):
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000312 self.assertEqual(re.compile("(?i)(a)(b)").pattern, "(?i)(a)(b)")
Antoine Pitroufd036452008-08-19 17:56:33 +0000313 self.assertEqual(re.compile("(?i)(a)(b)").flags, re.I | re.U)
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000314 self.assertEqual(re.compile("(?i)(a)(b)").groups, 2)
315 self.assertEqual(re.compile("(?i)(a)(b)").groupindex, {})
316 self.assertEqual(re.compile("(?i)(?P<first>a)(?P<other>b)").groupindex,
317 {'first': 1, 'other': 2})
318
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000319 self.assertEqual(re.match("(a)", "a").pos, 0)
320 self.assertEqual(re.match("(a)", "a").endpos, 1)
321 self.assertEqual(re.match("(a)", "a").string, "a")
322 self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1)))
323 self.assertNotEqual(re.match("(a)", "a").re, None)
324
325 def test_special_escapes(self):
326 self.assertEqual(re.search(r"\b(b.)\b",
327 "abcd abc bcd bx").group(1), "bx")
328 self.assertEqual(re.search(r"\B(b.)\B",
329 "abc bcd bc abxd").group(1), "bx")
330 self.assertEqual(re.search(r"\b(b.)\b",
331 "abcd abc bcd bx", re.LOCALE).group(1), "bx")
332 self.assertEqual(re.search(r"\B(b.)\B",
333 "abc bcd bc abxd", re.LOCALE).group(1), "bx")
334 self.assertEqual(re.search(r"\b(b.)\b",
335 "abcd abc bcd bx", re.UNICODE).group(1), "bx")
336 self.assertEqual(re.search(r"\B(b.)\B",
337 "abc bcd bc abxd", re.UNICODE).group(1), "bx")
338 self.assertEqual(re.search(r"^abc$", "\nabc\n", re.M).group(0), "abc")
339 self.assertEqual(re.search(r"^\Aabc\Z$", "abc", re.M).group(0), "abc")
340 self.assertEqual(re.search(r"^\Aabc\Z$", "\nabc\n", re.M), None)
341 self.assertEqual(re.search(r"\b(b.)\b",
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000342 "abcd abc bcd bx").group(1), "bx")
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000343 self.assertEqual(re.search(r"\B(b.)\B",
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000344 "abc bcd bc abxd").group(1), "bx")
345 self.assertEqual(re.search(r"^abc$", "\nabc\n", re.M).group(0), "abc")
346 self.assertEqual(re.search(r"^\Aabc\Z$", "abc", re.M).group(0), "abc")
347 self.assertEqual(re.search(r"^\Aabc\Z$", "\nabc\n", re.M), None)
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000348 self.assertEqual(re.search(r"\d\D\w\W\s\S",
349 "1aa! a").group(0), "1aa! a")
350 self.assertEqual(re.search(r"\d\D\w\W\s\S",
351 "1aa! a", re.LOCALE).group(0), "1aa! a")
352 self.assertEqual(re.search(r"\d\D\w\W\s\S",
353 "1aa! a", re.UNICODE).group(0), "1aa! a")
354
355 def test_ignore_case(self):
356 self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000357 self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000358
359 def test_bigcharset(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000360 self.assertEqual(re.match("([\u2222\u2223])",
361 "\u2222").group(1), "\u2222")
362 self.assertEqual(re.match("([\u2222\u2223])",
363 "\u2222", re.UNICODE).group(1), "\u2222")
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000364
365 def test_anyall(self):
366 self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
367 "a\nb")
368 self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
369 "a\n\nb")
370
371 def test_non_consuming(self):
372 self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
373 self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
374 self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
375 self.assertEqual(re.match("(a(?=\s[abc]*))", "a bc").group(1), "a")
376 self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a")
377 self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a")
378 self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a")
379
380 self.assertEqual(re.match(r"(a(?!\s[^a]))", "a a").group(1), "a")
381 self.assertEqual(re.match(r"(a(?!\s[abc]))", "a d").group(1), "a")
382 self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
383 self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
384
385 def test_ignore_case(self):
386 self.assertEqual(re.match(r"(a\s[^a])", "a b", re.I).group(1), "a b")
387 self.assertEqual(re.match(r"(a\s[^a]*)", "a bb", re.I).group(1), "a bb")
388 self.assertEqual(re.match(r"(a\s[abc])", "a b", re.I).group(1), "a b")
389 self.assertEqual(re.match(r"(a\s[abc]*)", "a bb", re.I).group(1), "a bb")
390 self.assertEqual(re.match(r"((a)\s\2)", "a a", re.I).group(1), "a a")
391 self.assertEqual(re.match(r"((a)\s\2*)", "a aa", re.I).group(1), "a aa")
392 self.assertEqual(re.match(r"((a)\s(abc|a))", "a a", re.I).group(1), "a a")
393 self.assertEqual(re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1), "a aa")
394
395 def test_category(self):
396 self.assertEqual(re.match(r"(\s)", " ").group(1), " ")
397
398 def test_getlower(self):
399 import _sre
400 self.assertEqual(_sre.getlower(ord('A'), 0), ord('a'))
401 self.assertEqual(_sre.getlower(ord('A'), re.LOCALE), ord('a'))
402 self.assertEqual(_sre.getlower(ord('A'), re.UNICODE), ord('a'))
403
404 self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000405 self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000406
407 def test_not_literal(self):
408 self.assertEqual(re.search("\s([^a])", " b").group(1), "b")
409 self.assertEqual(re.search("\s([^a]*)", " bb").group(1), "bb")
410
411 def test_search_coverage(self):
412 self.assertEqual(re.search("\s(b)", " b").group(1), "b")
413 self.assertEqual(re.search("a\s", "a ").group(0), "a ")
414
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000415 def test_re_escape(self):
416 p=""
417 for i in range(0, 256):
418 p = p + chr(i)
419 self.assertEqual(re.match(re.escape(chr(i)), chr(i)) is not None,
420 True)
421 self.assertEqual(re.match(re.escape(chr(i)), chr(i)).span(), (0,1))
Guido van Rossum49946571997-07-18 04:26:25 +0000422
Skip Montanaro1e703c62003-04-25 15:40:28 +0000423 pat=re.compile(re.escape(p))
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000424 self.assertEqual(pat.match(p) is not None, True)
425 self.assertEqual(pat.match(p).span(), (0,256))
Guido van Rossum49946571997-07-18 04:26:25 +0000426
Skip Montanaro1e703c62003-04-25 15:40:28 +0000427 def pickle_test(self, pickle):
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000428 oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
429 s = pickle.dumps(oldpat)
430 newpat = pickle.loads(s)
431 self.assertEqual(oldpat, newpat)
Guido van Rossum23b22571997-07-17 22:36:14 +0000432
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000433 def test_constants(self):
434 self.assertEqual(re.I, re.IGNORECASE)
435 self.assertEqual(re.L, re.LOCALE)
436 self.assertEqual(re.M, re.MULTILINE)
437 self.assertEqual(re.S, re.DOTALL)
438 self.assertEqual(re.X, re.VERBOSE)
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000439
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000440 def test_flags(self):
Skip Montanaro1e703c62003-04-25 15:40:28 +0000441 for flag in [re.I, re.M, re.X, re.S, re.L]:
442 self.assertNotEqual(re.compile('^pattern$', flag), None)
Guido van Rossumf473cb01998-01-14 16:42:17 +0000443
Skip Montanaro7d9963f2003-04-25 14:12:40 +0000444 def test_sre_character_literals(self):
445 for i in [0, 8, 16, 32, 64, 127, 128, 255]:
446 self.assertNotEqual(re.match(r"\%03o" % i, chr(i)), None)
447 self.assertNotEqual(re.match(r"\%03o0" % i, chr(i)+"0"), None)
448 self.assertNotEqual(re.match(r"\%03o8" % i, chr(i)+"8"), None)
449 self.assertNotEqual(re.match(r"\x%02x" % i, chr(i)), None)
450 self.assertNotEqual(re.match(r"\x%02x0" % i, chr(i)+"0"), None)
451 self.assertNotEqual(re.match(r"\x%02xz" % i, chr(i)+"z"), None)
452 self.assertRaises(re.error, re.match, "\911", "")
453
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000454 def test_sre_character_class_literals(self):
455 for i in [0, 8, 16, 32, 64, 127, 128, 255]:
456 self.assertNotEqual(re.match(r"[\%03o]" % i, chr(i)), None)
457 self.assertNotEqual(re.match(r"[\%03o0]" % i, chr(i)), None)
458 self.assertNotEqual(re.match(r"[\%03o8]" % i, chr(i)), None)
459 self.assertNotEqual(re.match(r"[\x%02x]" % i, chr(i)), None)
460 self.assertNotEqual(re.match(r"[\x%02x0]" % i, chr(i)), None)
461 self.assertNotEqual(re.match(r"[\x%02xz]" % i, chr(i)), None)
462 self.assertRaises(re.error, re.match, "[\911]", "")
463
Skip Montanaro7d9963f2003-04-25 14:12:40 +0000464 def test_bug_113254(self):
465 self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1)
466 self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1)
467 self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1))
468
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000469 def test_bug_527371(self):
470 # bug described in patches 527371/672491
471 self.assertEqual(re.match(r'(a)?a','a').lastindex, None)
472 self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
473 self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
474 self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
475 self.assertEqual(re.match("((a))", "a").lastindex, 1)
476
477 def test_bug_545855(self):
478 # bug 545855 -- This pattern failed to cause a compile error as it
479 # should, instead provoking a TypeError.
480 self.assertRaises(re.error, re.compile, 'foo[a-')
481
482 def test_bug_418626(self):
483 # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
484 # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
485 # pattern '*?' on a long string.
486 self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001)
487 self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0),
488 20003)
489 self.assertEqual(re.match('.*?cd', 20000*'abc'+'de').end(0), 60001)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000490 # non-simple '*?' still used to hit the recursion limit, before the
Tim Peters58eb11c2004-01-18 20:29:55 +0000491 # non-recursive scheme was implemented.
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000492 self.assertEqual(re.search('(a|b)*?c', 10000*'ab'+'cd').end(0), 20001)
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000493
494 def test_bug_612074(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000495 pat="["+re.escape("\u2039")+"]"
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000496 self.assertEqual(re.compile(pat) and 1, 1)
497
Skip Montanaro1e703c62003-04-25 15:40:28 +0000498 def test_stack_overflow(self):
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000499 # nasty cases that used to overflow the straightforward recursive
Skip Montanaro1e703c62003-04-25 15:40:28 +0000500 # implementation of repeated groups.
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000501 self.assertEqual(re.match('(x)*', 50000*'x').group(1), 'x')
502 self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
503 self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
Skip Montanaro1e703c62003-04-25 15:40:28 +0000504
505 def test_scanner(self):
506 def s_ident(scanner, token): return token
507 def s_operator(scanner, token): return "op%s" % token
508 def s_float(scanner, token): return float(token)
509 def s_int(scanner, token): return int(token)
510
511 scanner = Scanner([
512 (r"[a-zA-Z_]\w*", s_ident),
513 (r"\d+\.\d*", s_float),
514 (r"\d+", s_int),
515 (r"=|\+|-|\*|/", s_operator),
516 (r"\s+", None),
517 ])
518
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000519 self.assertNotEqual(scanner.scanner.scanner("").pattern, None)
520
Skip Montanaro1e703c62003-04-25 15:40:28 +0000521 self.assertEqual(scanner.scan("sum = 3*foo + 312.50 + bar"),
522 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
523 'op+', 'bar'], ''))
524
Skip Montanaro5ba00542003-04-25 16:00:14 +0000525 def test_bug_448951(self):
526 # bug 448951 (similar to 429357, but with single char match)
527 # (Also test greedy matches.)
528 for op in '','?','*':
529 self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(),
530 (None, None))
531 self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(),
532 ('a:', 'a'))
533
Gustavo Niemeyerc34f2552003-04-27 12:34:14 +0000534 def test_bug_725106(self):
535 # capturing groups in alternatives in repeats
536 self.assertEqual(re.match('^((a)|b)*', 'abc').groups(),
537 ('b', 'a'))
538 self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(),
539 ('c', 'b'))
540 self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(),
541 ('b', None))
542 self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(),
543 ('b', None))
544 self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(),
545 ('b', 'a'))
546 self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(),
547 ('c', 'b'))
548 self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(),
549 ('b', None))
550 self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(),
551 ('b', None))
552
Gustavo Niemeyer3646ab92003-04-27 13:25:21 +0000553 def test_bug_725149(self):
554 # mark_stack_base restoring before restoring marks
555 self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(),
556 ('a', None))
557 self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(),
558 ('a', None, None))
559
Just van Rossum12723ba2003-07-02 20:03:04 +0000560 def test_bug_764548(self):
561 # bug 764548, re.compile() barfs on str/unicode subclasses
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000562 class my_unicode(str): pass
Just van Rossum12723ba2003-07-02 20:03:04 +0000563 pat = re.compile(my_unicode("abc"))
564 self.assertEqual(pat.match("xyz"), None)
565
Skip Montanaro5ba00542003-04-25 16:00:14 +0000566 def test_finditer(self):
567 iter = re.finditer(r":+", "a:b::c:::d")
568 self.assertEqual([item.group(0) for item in iter],
569 [":", "::", ":::"])
570
Thomas Wouters40a088d2008-03-18 20:19:54 +0000571 def test_bug_926075(self):
572 self.assert_(re.compile('bug_926075') is not
573 re.compile(b'bug_926075'))
Hye-Shik Chang9f62ecc2004-04-20 21:30:07 +0000574
Martin v. Löwis7d9c6c72004-05-07 07:18:13 +0000575 def test_bug_931848(self):
Guido van Rossum7ebb9702007-05-15 21:39:58 +0000576 pattern = eval('"[\u002E\u3002\uFF0E\uFF61]"')
Martin v. Löwis7d9c6c72004-05-07 07:18:13 +0000577 self.assertEqual(re.compile(pattern).split("a.b.c"),
578 ['a','b','c'])
579
Gustavo Niemeyer0506c642004-09-03 18:11:59 +0000580 def test_bug_581080(self):
581 iter = re.finditer(r"\s", "a b")
Georg Brandla18af4e2007-04-21 15:47:16 +0000582 self.assertEqual(next(iter).span(), (1,2))
583 self.assertRaises(StopIteration, next, iter)
Gustavo Niemeyer0506c642004-09-03 18:11:59 +0000584
585 scanner = re.compile(r"\s").scanner("a b")
586 self.assertEqual(scanner.search().span(), (1, 2))
587 self.assertEqual(scanner.search(), None)
588
589 def test_bug_817234(self):
590 iter = re.finditer(r".*", "asdf")
Georg Brandla18af4e2007-04-21 15:47:16 +0000591 self.assertEqual(next(iter).span(), (0, 4))
592 self.assertEqual(next(iter).span(), (4, 4))
593 self.assertRaises(StopIteration, next, iter)
Gustavo Niemeyer0506c642004-09-03 18:11:59 +0000594
Guido van Rossumd8faa362007-04-27 19:54:29 +0000595 def test_empty_array(self):
596 # SF buf 1647541
597 import array
Guido van Rossum166746c2007-07-03 15:39:16 +0000598 for typecode in 'bBuhHiIlLfd':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000599 a = array.array(typecode)
Antoine Pitroufd036452008-08-19 17:56:33 +0000600 self.assertEqual(re.compile(b"bla").match(a), None)
601 self.assertEqual(re.compile(b"").match(a).groups(), ())
Gustavo Niemeyer0506c642004-09-03 18:11:59 +0000602
Christian Heimes072c0f12008-01-03 23:01:04 +0000603 def test_inline_flags(self):
604 # Bug #1700
Christian Heimes2e1d0f02008-01-04 00:47:51 +0000605 upper_char = chr(0x1ea0) # Latin Capital Letter A with Dot Bellow
606 lower_char = chr(0x1ea1) # Latin Small Letter A with Dot Bellow
Christian Heimes072c0f12008-01-03 23:01:04 +0000607
608 p = re.compile(upper_char, re.I | re.U)
609 q = p.match(lower_char)
610 self.assertNotEqual(q, None)
611
612 p = re.compile(lower_char, re.I | re.U)
613 q = p.match(upper_char)
614 self.assertNotEqual(q, None)
615
616 p = re.compile('(?i)' + upper_char, re.U)
617 q = p.match(lower_char)
618 self.assertNotEqual(q, None)
619
620 p = re.compile('(?i)' + lower_char, re.U)
621 q = p.match(upper_char)
622 self.assertNotEqual(q, None)
623
624 p = re.compile('(?iu)' + upper_char)
625 q = p.match(lower_char)
626 self.assertNotEqual(q, None)
627
628 p = re.compile('(?iu)' + lower_char)
629 q = p.match(upper_char)
630 self.assertNotEqual(q, None)
631
Christian Heimes25bb7832008-01-11 16:17:00 +0000632 def test_dollar_matches_twice(self):
633 "$ matches the end of string, and just before the terminating \n"
634 pattern = re.compile('$')
635 self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#')
636 self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#')
637 self.assertEqual(pattern.sub('#', '\n'), '#\n#')
638
639 pattern = re.compile('$', re.MULTILINE)
640 self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' )
641 self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#')
642 self.assertEqual(pattern.sub('#', '\n'), '#\n#')
643
Antoine Pitroufd036452008-08-19 17:56:33 +0000644 def test_bytes_str_mixing(self):
645 # Mixing str and bytes is disallowed
646 pat = re.compile('.')
647 bpat = re.compile(b'.')
648 self.assertRaises(TypeError, pat.match, b'b')
649 self.assertRaises(TypeError, bpat.match, 'b')
650 self.assertRaises(TypeError, pat.sub, b'b', 'c')
651 self.assertRaises(TypeError, pat.sub, 'b', b'c')
652 self.assertRaises(TypeError, pat.sub, b'b', b'c')
653 self.assertRaises(TypeError, bpat.sub, b'b', 'c')
654 self.assertRaises(TypeError, bpat.sub, 'b', b'c')
655 self.assertRaises(TypeError, bpat.sub, 'b', 'c')
656
657 def test_ascii_and_unicode_flag(self):
658 # String patterns
659 for flags in (0, re.UNICODE):
660 pat = re.compile('\xc0', flags | re.IGNORECASE)
661 self.assertNotEqual(pat.match('\xe0'), None)
662 pat = re.compile('\w', flags)
663 self.assertNotEqual(pat.match('\xe0'), None)
664 pat = re.compile('\xc0', re.ASCII | re.IGNORECASE)
665 self.assertEqual(pat.match('\xe0'), None)
666 pat = re.compile('(?a)\xc0', re.IGNORECASE)
667 self.assertEqual(pat.match('\xe0'), None)
668 pat = re.compile('\w', re.ASCII)
669 self.assertEqual(pat.match('\xe0'), None)
670 pat = re.compile('(?a)\w')
671 self.assertEqual(pat.match('\xe0'), None)
672 # Bytes patterns
673 for flags in (0, re.ASCII):
674 pat = re.compile(b'\xc0', re.IGNORECASE)
675 self.assertEqual(pat.match(b'\xe0'), None)
676 pat = re.compile(b'\w')
677 self.assertEqual(pat.match(b'\xe0'), None)
678 # Incompatibilities
679 self.assertRaises(ValueError, re.compile, b'\w', re.UNICODE)
680 self.assertRaises(ValueError, re.compile, b'(?u)\w')
681 self.assertRaises(ValueError, re.compile, '\w', re.UNICODE | re.ASCII)
682 self.assertRaises(ValueError, re.compile, '(?u)\w', re.ASCII)
683 self.assertRaises(ValueError, re.compile, '(?a)\w', re.UNICODE)
684 self.assertRaises(ValueError, re.compile, '(?au)\w')
685
Christian Heimes072c0f12008-01-03 23:01:04 +0000686
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000687def run_re_tests():
688 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
689 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000690 print('Running re_tests test suite')
Guido van Rossum8e0ce301997-07-11 19:34:44 +0000691 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000692 # To save time, only run the first and last 10 tests
693 #tests = tests[:10] + tests[-10:]
694 pass
Guido van Rossum8e0ce301997-07-11 19:34:44 +0000695
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000696 for t in tests:
697 sys.stdout.flush()
698 pattern = s = outcome = repl = expected = None
699 if len(t) == 5:
700 pattern, s, outcome, repl, expected = t
701 elif len(t) == 3:
702 pattern, s, outcome = t
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000703 else:
Collin Winter3add4d72007-08-29 23:37:32 +0000704 raise ValueError('Test tuples should have 3 or 5 fields', t)
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000705
Guido van Rossum41360a41998-03-26 19:42:58 +0000706 try:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000707 obj = re.compile(pattern)
708 except re.error:
709 if outcome == SYNTAX_ERROR: pass # Expected a syntax error
Guido van Rossum41360a41998-03-26 19:42:58 +0000710 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000711 print('=== Syntax error:', t)
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000712 except KeyboardInterrupt: raise KeyboardInterrupt
713 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000714 print('*** Unexpected error ***', t)
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000715 if verbose:
716 traceback.print_exc(file=sys.stdout)
717 else:
Fredrik Lundh17741be2001-03-22 15:51:28 +0000718 try:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000719 result = obj.search(s)
Guido van Rossumb940e112007-01-10 16:19:56 +0000720 except re.error as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000721 print('=== Unexpected exception', t, repr(msg))
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000722 if outcome == SYNTAX_ERROR:
723 # This should have been a syntax error; forget it.
724 pass
725 elif outcome == FAIL:
726 if result is None: pass # No match, as expected
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000727 else: print('=== Succeeded incorrectly', t)
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000728 elif outcome == SUCCEED:
729 if result is not None:
730 # Matched, as expected, so now we compute the
731 # result string and compare it to our expected result.
732 start, end = result.span(0)
733 vardict={'found': result.group(0),
734 'groups': result.group(),
735 'flags': result.re.flags}
736 for i in range(1, 100):
737 try:
738 gi = result.group(i)
739 # Special hack because else the string concat fails:
740 if gi is None:
741 gi = "None"
742 except IndexError:
743 gi = "Error"
744 vardict['g%d' % i] = gi
745 for i in result.re.groupindex.keys():
746 try:
747 gi = result.group(i)
748 if gi is None:
749 gi = "None"
750 except IndexError:
751 gi = "Error"
752 vardict[i] = gi
753 repl = eval(repl, vardict)
754 if repl != expected:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000755 print('=== grouping error', t, end=' ')
756 print(repr(repl) + ' should be ' + repr(expected))
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000757 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000758 print('=== Failed incorrectly', t)
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000759
Antoine Pitrou22628c42008-07-22 17:53:22 +0000760 # Try the match with both pattern and string converted to
761 # bytes, and check that it still succeeds.
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000762 try:
Antoine Pitrou22628c42008-07-22 17:53:22 +0000763 bpat = bytes(pattern, "ascii")
764 bs = bytes(s, "ascii")
765 except UnicodeEncodeError:
766 # skip non-ascii tests
767 pass
768 else:
769 try:
770 bpat = re.compile(bpat)
771 except Exception:
772 print('=== Fails on bytes pattern compile', t)
773 if verbose:
774 traceback.print_exc(file=sys.stdout)
775 else:
776 bytes_result = bpat.search(bs)
777 if bytes_result is None:
778 print('=== Fails on bytes pattern match', t)
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000779
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000780 # Try the match with the search area limited to the extent
781 # of the match and see if it still succeeds. \B will
782 # break (because it won't match at the end or start of a
783 # string), so we'll ignore patterns that feature it.
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000784
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000785 if pattern[:2] != '\\B' and pattern[-2:] != '\\B' \
786 and result is not None:
787 obj = re.compile(pattern)
788 result = obj.search(s, result.start(0), result.end(0) + 1)
789 if result is None:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000790 print('=== Failed on range-limited match', t)
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000791
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000792 # Try the match with IGNORECASE enabled, and check that it
793 # still succeeds.
794 obj = re.compile(pattern, re.IGNORECASE)
795 result = obj.search(s)
Fred Drake132dce22000-12-12 23:11:42 +0000796 if result is None:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000797 print('=== Fails on case-insensitive match', t)
Guido van Rossumdfa67901997-12-08 17:12:06 +0000798
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000799 # Try the match with LOCALE enabled, and check that it
800 # still succeeds.
Antoine Pitrou22628c42008-07-22 17:53:22 +0000801 if '(?u)' not in pattern:
802 obj = re.compile(pattern, re.LOCALE)
803 result = obj.search(s)
804 if result is None:
805 print('=== Fails on locale-sensitive match', t)
Guido van Rossumdfa67901997-12-08 17:12:06 +0000806
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000807 # Try the match with UNICODE locale enabled, and check
808 # that it still succeeds.
809 obj = re.compile(pattern, re.UNICODE)
810 result = obj.search(s)
811 if result is None:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000812 print('=== Fails on unicode-sensitive match', t)
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000813
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000814def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000815 run_unittest(ReTests)
Skip Montanaro1e703c62003-04-25 15:40:28 +0000816 run_re_tests()
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000817
818if __name__ == "__main__":
819 test_main()