blob: dafd82e98a92a9d634512b39e242cf9f114f0508 [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
Walter Dörwald21d3a322003-05-01 17:45:56 +00004from test.test_support import verbose, run_unittest
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
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +000086 def test_sub_template_numeric_escape(self):
87 # bug 776311 and friends
88 self.assertEqual(re.sub('x', r'\0', 'x'), '\0')
89 self.assertEqual(re.sub('x', r'\000', 'x'), '\000')
90 self.assertEqual(re.sub('x', r'\001', 'x'), '\001')
91 self.assertEqual(re.sub('x', r'\008', 'x'), '\0' + '8')
92 self.assertEqual(re.sub('x', r'\009', 'x'), '\0' + '9')
93 self.assertEqual(re.sub('x', r'\111', 'x'), '\111')
94 self.assertEqual(re.sub('x', r'\117', 'x'), '\117')
95
96 self.assertEqual(re.sub('x', r'\1111', 'x'), '\1111')
97 self.assertEqual(re.sub('x', r'\1111', 'x'), '\111' + '1')
98
99 self.assertEqual(re.sub('x', r'\00', 'x'), '\x00')
100 self.assertEqual(re.sub('x', r'\07', 'x'), '\x07')
101 self.assertEqual(re.sub('x', r'\08', 'x'), '\0' + '8')
102 self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9')
103 self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a')
104
105 self.assertEqual(re.sub('x', r'\400', 'x'), '\0')
106 self.assertEqual(re.sub('x', r'\777', 'x'), '\377')
Tim Peters0e9980f2004-09-12 03:49:31 +0000107
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000108 self.assertRaises(re.error, re.sub, 'x', r'\1', 'x')
109 self.assertRaises(re.error, re.sub, 'x', r'\8', 'x')
110 self.assertRaises(re.error, re.sub, 'x', r'\9', 'x')
111 self.assertRaises(re.error, re.sub, 'x', r'\11', 'x')
112 self.assertRaises(re.error, re.sub, 'x', r'\18', 'x')
113 self.assertRaises(re.error, re.sub, 'x', r'\1a', 'x')
114 self.assertRaises(re.error, re.sub, 'x', r'\90', 'x')
115 self.assertRaises(re.error, re.sub, 'x', r'\99', 'x')
116 self.assertRaises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8'
117 self.assertRaises(re.error, re.sub, 'x', r'\11a', 'x')
118 self.assertRaises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1'
119 self.assertRaises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0'
120
121 # in python2.3 (etc), these loop endlessly in sre_parser.py
122 self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x')
123 self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz'),
124 'xz8')
125 self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz'),
126 'xza')
127
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000128 def test_qualified_re_sub(self):
129 self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb')
130 self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa')
Guido van Rossum8430c581998-04-03 21:47:12 +0000131
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000132 def test_bug_114660(self):
133 self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there'),
134 'hello there')
135
136 def test_bug_462270(self):
137 # Test for empty sub() behaviour, see SF bug #462270
138 self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-')
139 self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d')
140
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000141 def test_symbolic_refs(self):
142 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
143 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
144 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
145 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
146 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
147 self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
148 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
149 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx')
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000150 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx')
Guido van Rossumf473cb01998-01-14 16:42:17 +0000151
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000152 def test_re_subn(self):
153 self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
154 self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1))
155 self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0))
156 self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4))
157 self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2))
Guido van Rossum49946571997-07-18 04:26:25 +0000158
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000159 def test_re_split(self):
160 self.assertEqual(re.split(":", ":a:b::c"), ['', 'a', 'b', '', 'c'])
161 self.assertEqual(re.split(":*", ":a:b::c"), ['', 'a', 'b', 'c'])
162 self.assertEqual(re.split("(:*)", ":a:b::c"),
163 ['', ':', 'a', ':', 'b', '::', 'c'])
164 self.assertEqual(re.split("(?::*)", ":a:b::c"), ['', 'a', 'b', 'c'])
165 self.assertEqual(re.split("(:)*", ":a:b::c"),
166 ['', ':', 'a', ':', 'b', ':', 'c'])
167 self.assertEqual(re.split("([b:]+)", ":a:b::c"),
168 ['', ':', 'a', ':b::', 'c'])
169 self.assertEqual(re.split("(b)|(:+)", ":a:b::c"),
170 ['', None, ':', 'a', None, ':', '', 'b', None, '',
171 None, '::', 'c'])
172 self.assertEqual(re.split("(?:b)|(?::+)", ":a:b::c"),
173 ['', 'a', '', '', 'c'])
Guido van Rossum49946571997-07-18 04:26:25 +0000174
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000175 def test_qualified_re_split(self):
176 self.assertEqual(re.split(":", ":a:b::c", 2), ['', 'a', 'b::c'])
177 self.assertEqual(re.split(':', 'a:b:c:d', 2), ['a', 'b', 'c:d'])
178 self.assertEqual(re.split("(:)", ":a:b::c", 2),
179 ['', ':', 'a', ':', 'b::c'])
180 self.assertEqual(re.split("(:*)", ":a:b::c", 2),
181 ['', ':', 'a', ':', 'b::c'])
Guido van Rossum49946571997-07-18 04:26:25 +0000182
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000183 def test_re_findall(self):
184 self.assertEqual(re.findall(":+", "abc"), [])
185 self.assertEqual(re.findall(":+", "a:b::c:::d"), [":", "::", ":::"])
186 self.assertEqual(re.findall("(:+)", "a:b::c:::d"), [":", "::", ":::"])
187 self.assertEqual(re.findall("(:)(:*)", "a:b::c:::d"), [(":", ""),
188 (":", ":"),
189 (":", "::")])
Guido van Rossum49946571997-07-18 04:26:25 +0000190
Skip Montanaro5ba00542003-04-25 16:00:14 +0000191 def test_bug_117612(self):
192 self.assertEqual(re.findall(r"(a|(b))", "aba"),
193 [("a", ""),("b", "b"),("a", "")])
194
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000195 def test_re_match(self):
Skip Montanaro5ba00542003-04-25 16:00:14 +0000196 self.assertEqual(re.match('a', 'a').groups(), ())
197 self.assertEqual(re.match('(a)', 'a').groups(), ('a',))
198 self.assertEqual(re.match(r'(a)', 'a').group(0), 'a')
199 self.assertEqual(re.match(r'(a)', 'a').group(1), 'a')
200 self.assertEqual(re.match(r'(a)', 'a').group(1, 1), ('a', 'a'))
Guido van Rossum49946571997-07-18 04:26:25 +0000201
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000202 pat = re.compile('((a)|(b))(c)?')
203 self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None))
204 self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None))
205 self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c'))
206 self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
207 self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
Guido van Rossum8430c581998-04-03 21:47:12 +0000208
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000209 # A single group
210 m = re.match('(a)', 'a')
211 self.assertEqual(m.group(0), 'a')
212 self.assertEqual(m.group(0), 'a')
213 self.assertEqual(m.group(1), 'a')
214 self.assertEqual(m.group(1, 1), ('a', 'a'))
Guido van Rossum49946571997-07-18 04:26:25 +0000215
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000216 pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
217 self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
218 self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
219 (None, 'b', None))
220 self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
Guido van Rossum49946571997-07-18 04:26:25 +0000221
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000222 def test_re_groupref_exists(self):
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000223 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
224 ('(', 'a'))
225 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
226 (None, 'a'))
227 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None)
228 self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None)
229 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
230 ('a', 'b'))
231 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
232 (None, 'd'))
233 self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
234 (None, 'd'))
235 self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(),
236 ('a', ''))
237
Michael W. Hudsone7fa1af2005-06-03 13:55:58 +0000238 # Tests for bug #1177831: exercise groups other than the first group
239 p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
240 self.assertEqual(p.match('abc').groups(),
241 ('a', 'b', 'c'))
242 self.assertEqual(p.match('ad').groups(),
243 ('a', None, 'd'))
244 self.assertEqual(p.match('abd'), None)
245 self.assertEqual(p.match('ac'), None)
246
Andrew M. Kuchling3554cad2005-06-02 13:38:45 +0000247
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000248 def test_re_groupref(self):
249 self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
250 ('|', 'a'))
251 self.assertEqual(re.match(r'^(\|)?([^()]+)\1?$', 'a').groups(),
252 (None, 'a'))
253 self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', 'a|'), None)
254 self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a'), None)
255 self.assertEqual(re.match(r'^(?:(a)|c)(\1)$', 'aa').groups(),
256 ('a', 'a'))
257 self.assertEqual(re.match(r'^(?:(a)|c)(\1)?$', 'c').groups(),
258 (None, None))
259
260 def test_groupdict(self):
261 self.assertEqual(re.match('(?P<first>first) (?P<second>second)',
262 'first second').groupdict(),
263 {'first':'first', 'second':'second'})
264
265 def test_expand(self):
266 self.assertEqual(re.match("(?P<first>first) (?P<second>second)",
267 "first second")
268 .expand(r"\2 \1 \g<second> \g<first>"),
269 "second first second first")
270
271 def test_repeat_minmax(self):
272 self.assertEqual(re.match("^(\w){1}$", "abc"), None)
273 self.assertEqual(re.match("^(\w){1}?$", "abc"), None)
274 self.assertEqual(re.match("^(\w){1,2}$", "abc"), None)
275 self.assertEqual(re.match("^(\w){1,2}?$", "abc"), None)
276
277 self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
278 self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
279 self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
280 self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
281 self.assertEqual(re.match("^(\w){3}?$", "abc").group(1), "c")
282 self.assertEqual(re.match("^(\w){1,3}?$", "abc").group(1), "c")
283 self.assertEqual(re.match("^(\w){1,4}?$", "abc").group(1), "c")
284 self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
285
286 self.assertEqual(re.match("^x{1}$", "xxx"), None)
287 self.assertEqual(re.match("^x{1}?$", "xxx"), None)
288 self.assertEqual(re.match("^x{1,2}$", "xxx"), None)
289 self.assertEqual(re.match("^x{1,2}?$", "xxx"), None)
290
291 self.assertNotEqual(re.match("^x{3}$", "xxx"), None)
292 self.assertNotEqual(re.match("^x{1,3}$", "xxx"), None)
293 self.assertNotEqual(re.match("^x{1,4}$", "xxx"), None)
294 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)
295 self.assertNotEqual(re.match("^x{3}?$", "xxx"), None)
296 self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None)
297 self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None)
298 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)
299
Gustavo Niemeyer6fa0c5a2005-09-14 08:54:39 +0000300 self.assertEqual(re.match("^x{}$", "xxx"), None)
301 self.assertNotEqual(re.match("^x{}$", "x{}"), None)
302
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000303 def test_getattr(self):
304 self.assertEqual(re.match("(a)", "a").pos, 0)
305 self.assertEqual(re.match("(a)", "a").endpos, 1)
306 self.assertEqual(re.match("(a)", "a").string, "a")
307 self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1)))
308 self.assertNotEqual(re.match("(a)", "a").re, None)
309
310 def test_special_escapes(self):
311 self.assertEqual(re.search(r"\b(b.)\b",
312 "abcd abc bcd bx").group(1), "bx")
313 self.assertEqual(re.search(r"\B(b.)\B",
314 "abc bcd bc abxd").group(1), "bx")
315 self.assertEqual(re.search(r"\b(b.)\b",
316 "abcd abc bcd bx", re.LOCALE).group(1), "bx")
317 self.assertEqual(re.search(r"\B(b.)\B",
318 "abc bcd bc abxd", re.LOCALE).group(1), "bx")
319 self.assertEqual(re.search(r"\b(b.)\b",
320 "abcd abc bcd bx", re.UNICODE).group(1), "bx")
321 self.assertEqual(re.search(r"\B(b.)\B",
322 "abc bcd bc abxd", re.UNICODE).group(1), "bx")
323 self.assertEqual(re.search(r"^abc$", "\nabc\n", re.M).group(0), "abc")
324 self.assertEqual(re.search(r"^\Aabc\Z$", "abc", re.M).group(0), "abc")
325 self.assertEqual(re.search(r"^\Aabc\Z$", "\nabc\n", re.M), None)
326 self.assertEqual(re.search(r"\b(b.)\b",
327 u"abcd abc bcd bx").group(1), "bx")
328 self.assertEqual(re.search(r"\B(b.)\B",
329 u"abc bcd bc abxd").group(1), "bx")
330 self.assertEqual(re.search(r"^abc$", u"\nabc\n", re.M).group(0), "abc")
331 self.assertEqual(re.search(r"^\Aabc\Z$", u"abc", re.M).group(0), "abc")
332 self.assertEqual(re.search(r"^\Aabc\Z$", u"\nabc\n", re.M), None)
333 self.assertEqual(re.search(r"\d\D\w\W\s\S",
334 "1aa! a").group(0), "1aa! a")
335 self.assertEqual(re.search(r"\d\D\w\W\s\S",
336 "1aa! a", re.LOCALE).group(0), "1aa! a")
337 self.assertEqual(re.search(r"\d\D\w\W\s\S",
338 "1aa! a", re.UNICODE).group(0), "1aa! a")
339
340 def test_ignore_case(self):
341 self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
342 self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")
343
344 def test_bigcharset(self):
345 self.assertEqual(re.match(u"([\u2222\u2223])",
346 u"\u2222").group(1), u"\u2222")
347 self.assertEqual(re.match(u"([\u2222\u2223])",
348 u"\u2222", re.UNICODE).group(1), u"\u2222")
349
350 def test_anyall(self):
351 self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
352 "a\nb")
353 self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
354 "a\n\nb")
355
356 def test_non_consuming(self):
357 self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
358 self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
359 self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
360 self.assertEqual(re.match("(a(?=\s[abc]*))", "a bc").group(1), "a")
361 self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a")
362 self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a")
363 self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a")
364
365 self.assertEqual(re.match(r"(a(?!\s[^a]))", "a a").group(1), "a")
366 self.assertEqual(re.match(r"(a(?!\s[abc]))", "a d").group(1), "a")
367 self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
368 self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
369
370 def test_ignore_case(self):
371 self.assertEqual(re.match(r"(a\s[^a])", "a b", re.I).group(1), "a b")
372 self.assertEqual(re.match(r"(a\s[^a]*)", "a bb", re.I).group(1), "a bb")
373 self.assertEqual(re.match(r"(a\s[abc])", "a b", re.I).group(1), "a b")
374 self.assertEqual(re.match(r"(a\s[abc]*)", "a bb", re.I).group(1), "a bb")
375 self.assertEqual(re.match(r"((a)\s\2)", "a a", re.I).group(1), "a a")
376 self.assertEqual(re.match(r"((a)\s\2*)", "a aa", re.I).group(1), "a aa")
377 self.assertEqual(re.match(r"((a)\s(abc|a))", "a a", re.I).group(1), "a a")
378 self.assertEqual(re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1), "a aa")
379
380 def test_category(self):
381 self.assertEqual(re.match(r"(\s)", " ").group(1), " ")
382
383 def test_getlower(self):
384 import _sre
385 self.assertEqual(_sre.getlower(ord('A'), 0), ord('a'))
386 self.assertEqual(_sre.getlower(ord('A'), re.LOCALE), ord('a'))
387 self.assertEqual(_sre.getlower(ord('A'), re.UNICODE), ord('a'))
388
389 self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
390 self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")
391
392 def test_not_literal(self):
393 self.assertEqual(re.search("\s([^a])", " b").group(1), "b")
394 self.assertEqual(re.search("\s([^a]*)", " bb").group(1), "bb")
395
396 def test_search_coverage(self):
397 self.assertEqual(re.search("\s(b)", " b").group(1), "b")
398 self.assertEqual(re.search("a\s", "a ").group(0), "a ")
399
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000400 def test_re_escape(self):
401 p=""
402 for i in range(0, 256):
403 p = p + chr(i)
404 self.assertEqual(re.match(re.escape(chr(i)), chr(i)) is not None,
405 True)
406 self.assertEqual(re.match(re.escape(chr(i)), chr(i)).span(), (0,1))
Guido van Rossum49946571997-07-18 04:26:25 +0000407
Skip Montanaro1e703c62003-04-25 15:40:28 +0000408 pat=re.compile(re.escape(p))
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000409 self.assertEqual(pat.match(p) is not None, True)
410 self.assertEqual(pat.match(p).span(), (0,256))
Guido van Rossum49946571997-07-18 04:26:25 +0000411
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000412 def test_pickling(self):
413 import pickle
Skip Montanaro1e703c62003-04-25 15:40:28 +0000414 self.pickle_test(pickle)
Guido van Rossumbf12cdb2006-08-17 20:24:18 +0000415 try:
416 import cPickle
417 except ImportError:
418 pass # cPickle not found -- skip it
419 else:
420 self.pickle_test(cPickle)
Skip Montanaro1e703c62003-04-25 15:40:28 +0000421
422 def pickle_test(self, pickle):
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000423 oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
424 s = pickle.dumps(oldpat)
425 newpat = pickle.loads(s)
426 self.assertEqual(oldpat, newpat)
Guido van Rossum23b22571997-07-17 22:36:14 +0000427
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000428 def test_constants(self):
429 self.assertEqual(re.I, re.IGNORECASE)
430 self.assertEqual(re.L, re.LOCALE)
431 self.assertEqual(re.M, re.MULTILINE)
432 self.assertEqual(re.S, re.DOTALL)
433 self.assertEqual(re.X, re.VERBOSE)
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000434
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000435 def test_flags(self):
Skip Montanaro1e703c62003-04-25 15:40:28 +0000436 for flag in [re.I, re.M, re.X, re.S, re.L]:
437 self.assertNotEqual(re.compile('^pattern$', flag), None)
Guido van Rossumf473cb01998-01-14 16:42:17 +0000438
Skip Montanaro7d9963f2003-04-25 14:12:40 +0000439 def test_sre_character_literals(self):
440 for i in [0, 8, 16, 32, 64, 127, 128, 255]:
441 self.assertNotEqual(re.match(r"\%03o" % i, chr(i)), None)
442 self.assertNotEqual(re.match(r"\%03o0" % i, chr(i)+"0"), None)
443 self.assertNotEqual(re.match(r"\%03o8" % i, chr(i)+"8"), None)
444 self.assertNotEqual(re.match(r"\x%02x" % i, chr(i)), None)
445 self.assertNotEqual(re.match(r"\x%02x0" % i, chr(i)+"0"), None)
446 self.assertNotEqual(re.match(r"\x%02xz" % i, chr(i)+"z"), None)
447 self.assertRaises(re.error, re.match, "\911", "")
448
Gustavo Niemeyera01a2ee2004-09-03 17:06:10 +0000449 def test_sre_character_class_literals(self):
450 for i in [0, 8, 16, 32, 64, 127, 128, 255]:
451 self.assertNotEqual(re.match(r"[\%03o]" % i, chr(i)), None)
452 self.assertNotEqual(re.match(r"[\%03o0]" % i, chr(i)), None)
453 self.assertNotEqual(re.match(r"[\%03o8]" % i, chr(i)), None)
454 self.assertNotEqual(re.match(r"[\x%02x]" % i, chr(i)), None)
455 self.assertNotEqual(re.match(r"[\x%02x0]" % i, chr(i)), None)
456 self.assertNotEqual(re.match(r"[\x%02xz]" % i, chr(i)), None)
457 self.assertRaises(re.error, re.match, "[\911]", "")
458
Skip Montanaro7d9963f2003-04-25 14:12:40 +0000459 def test_bug_113254(self):
460 self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1)
461 self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1)
462 self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1))
463
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000464 def test_bug_527371(self):
465 # bug described in patches 527371/672491
466 self.assertEqual(re.match(r'(a)?a','a').lastindex, None)
467 self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
468 self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
469 self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
470 self.assertEqual(re.match("((a))", "a").lastindex, 1)
471
472 def test_bug_545855(self):
473 # bug 545855 -- This pattern failed to cause a compile error as it
474 # should, instead provoking a TypeError.
475 self.assertRaises(re.error, re.compile, 'foo[a-')
476
477 def test_bug_418626(self):
478 # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
479 # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
480 # pattern '*?' on a long string.
481 self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001)
482 self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0),
483 20003)
484 self.assertEqual(re.match('.*?cd', 20000*'abc'+'de').end(0), 60001)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000485 # non-simple '*?' still used to hit the recursion limit, before the
Tim Peters58eb11c2004-01-18 20:29:55 +0000486 # non-recursive scheme was implemented.
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000487 self.assertEqual(re.search('(a|b)*?c', 10000*'ab'+'cd').end(0), 20001)
Skip Montanaro2726fcd2003-04-25 14:31:54 +0000488
489 def test_bug_612074(self):
490 pat=u"["+re.escape(u"\u2039")+u"]"
491 self.assertEqual(re.compile(pat) and 1, 1)
492
Skip Montanaro1e703c62003-04-25 15:40:28 +0000493 def test_stack_overflow(self):
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000494 # nasty cases that used to overflow the straightforward recursive
Skip Montanaro1e703c62003-04-25 15:40:28 +0000495 # implementation of repeated groups.
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000496 self.assertEqual(re.match('(x)*', 50000*'x').group(1), 'x')
497 self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
498 self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
Skip Montanaro1e703c62003-04-25 15:40:28 +0000499
500 def test_scanner(self):
501 def s_ident(scanner, token): return token
502 def s_operator(scanner, token): return "op%s" % token
503 def s_float(scanner, token): return float(token)
504 def s_int(scanner, token): return int(token)
505
506 scanner = Scanner([
507 (r"[a-zA-Z_]\w*", s_ident),
508 (r"\d+\.\d*", s_float),
509 (r"\d+", s_int),
510 (r"=|\+|-|\*|/", s_operator),
511 (r"\s+", None),
512 ])
513
Gustavo Niemeyer25fe0bf2003-06-20 00:25:14 +0000514 self.assertNotEqual(scanner.scanner.scanner("").pattern, None)
515
Skip Montanaro1e703c62003-04-25 15:40:28 +0000516 self.assertEqual(scanner.scan("sum = 3*foo + 312.50 + bar"),
517 (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
518 'op+', 'bar'], ''))
519
Skip Montanaro5ba00542003-04-25 16:00:14 +0000520 def test_bug_448951(self):
521 # bug 448951 (similar to 429357, but with single char match)
522 # (Also test greedy matches.)
523 for op in '','?','*':
524 self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(),
525 (None, None))
526 self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(),
527 ('a:', 'a'))
528
Gustavo Niemeyerc34f2552003-04-27 12:34:14 +0000529 def test_bug_725106(self):
530 # capturing groups in alternatives in repeats
531 self.assertEqual(re.match('^((a)|b)*', 'abc').groups(),
532 ('b', 'a'))
533 self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(),
534 ('c', 'b'))
535 self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(),
536 ('b', None))
537 self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(),
538 ('b', None))
539 self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(),
540 ('b', 'a'))
541 self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(),
542 ('c', 'b'))
543 self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(),
544 ('b', None))
545 self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(),
546 ('b', None))
547
Gustavo Niemeyer3646ab92003-04-27 13:25:21 +0000548 def test_bug_725149(self):
549 # mark_stack_base restoring before restoring marks
550 self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(),
551 ('a', None))
552 self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(),
553 ('a', None, None))
554
Just van Rossum12723ba2003-07-02 20:03:04 +0000555 def test_bug_764548(self):
556 # bug 764548, re.compile() barfs on str/unicode subclasses
557 try:
558 unicode
559 except NameError:
560 return # no problem if we have no unicode
561 class my_unicode(unicode): pass
562 pat = re.compile(my_unicode("abc"))
563 self.assertEqual(pat.match("xyz"), None)
564
Skip Montanaro5ba00542003-04-25 16:00:14 +0000565 def test_finditer(self):
566 iter = re.finditer(r":+", "a:b::c:::d")
567 self.assertEqual([item.group(0) for item in iter],
568 [":", "::", ":::"])
569
Hye-Shik Chang9f62ecc2004-04-20 21:30:07 +0000570 def test_bug_926075(self):
571 try:
572 unicode
573 except NameError:
574 return # no problem if we have no unicode
575 self.assert_(re.compile('bug_926075') is not
576 re.compile(eval("u'bug_926075'")))
577
Martin v. Löwis7d9c6c72004-05-07 07:18:13 +0000578 def test_bug_931848(self):
579 try:
580 unicode
581 except NameError:
582 pass
583 pattern = eval('u"[\u002E\u3002\uFF0E\uFF61]"')
584 self.assertEqual(re.compile(pattern).split("a.b.c"),
585 ['a','b','c'])
586
Gustavo Niemeyer0506c642004-09-03 18:11:59 +0000587 def test_bug_581080(self):
588 iter = re.finditer(r"\s", "a b")
589 self.assertEqual(iter.next().span(), (1,2))
590 self.assertRaises(StopIteration, iter.next)
591
592 scanner = re.compile(r"\s").scanner("a b")
593 self.assertEqual(scanner.search().span(), (1, 2))
594 self.assertEqual(scanner.search(), None)
595
596 def test_bug_817234(self):
597 iter = re.finditer(r".*", "asdf")
598 self.assertEqual(iter.next().span(), (0, 4))
599 self.assertEqual(iter.next().span(), (4, 4))
600 self.assertRaises(StopIteration, iter.next)
601
602
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000603def run_re_tests():
604 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
605 if verbose:
606 print 'Running re_tests test suite'
Guido van Rossum8e0ce301997-07-11 19:34:44 +0000607 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000608 # To save time, only run the first and last 10 tests
609 #tests = tests[:10] + tests[-10:]
610 pass
Guido van Rossum8e0ce301997-07-11 19:34:44 +0000611
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000612 for t in tests:
613 sys.stdout.flush()
614 pattern = s = outcome = repl = expected = None
615 if len(t) == 5:
616 pattern, s, outcome, repl, expected = t
617 elif len(t) == 3:
618 pattern, s, outcome = t
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000619 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000620 raise ValueError, ('Test tuples should have 3 or 5 fields', t)
621
Guido van Rossum41360a41998-03-26 19:42:58 +0000622 try:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000623 obj = re.compile(pattern)
624 except re.error:
625 if outcome == SYNTAX_ERROR: pass # Expected a syntax error
Guido van Rossum41360a41998-03-26 19:42:58 +0000626 else:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000627 print '=== Syntax error:', t
628 except KeyboardInterrupt: raise KeyboardInterrupt
629 except:
630 print '*** Unexpected error ***', t
631 if verbose:
632 traceback.print_exc(file=sys.stdout)
633 else:
Fredrik Lundh17741be2001-03-22 15:51:28 +0000634 try:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000635 result = obj.search(s)
Guido van Rossumb940e112007-01-10 16:19:56 +0000636 except re.error as msg:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000637 print '=== Unexpected exception', t, repr(msg)
638 if outcome == SYNTAX_ERROR:
639 # This should have been a syntax error; forget it.
640 pass
641 elif outcome == FAIL:
642 if result is None: pass # No match, as expected
643 else: print '=== Succeeded incorrectly', t
644 elif outcome == SUCCEED:
645 if result is not None:
646 # Matched, as expected, so now we compute the
647 # result string and compare it to our expected result.
648 start, end = result.span(0)
649 vardict={'found': result.group(0),
650 'groups': result.group(),
651 'flags': result.re.flags}
652 for i in range(1, 100):
653 try:
654 gi = result.group(i)
655 # Special hack because else the string concat fails:
656 if gi is None:
657 gi = "None"
658 except IndexError:
659 gi = "Error"
660 vardict['g%d' % i] = gi
661 for i in result.re.groupindex.keys():
662 try:
663 gi = result.group(i)
664 if gi is None:
665 gi = "None"
666 except IndexError:
667 gi = "Error"
668 vardict[i] = gi
669 repl = eval(repl, vardict)
670 if repl != expected:
671 print '=== grouping error', t,
672 print repr(repl) + ' should be ' + repr(expected)
673 else:
674 print '=== Failed incorrectly', t
675
676 # Try the match on a unicode string, and check that it
677 # still succeeds.
678 try:
679 result = obj.search(unicode(s, "latin-1"))
680 if result is None:
681 print '=== Fails on unicode match', t
682 except NameError:
683 continue # 1.5.2
684 except TypeError:
685 continue # unicode test case
686
687 # Try the match on a unicode pattern, and check that it
688 # still succeeds.
689 obj=re.compile(unicode(pattern, "latin-1"))
690 result = obj.search(s)
Fredrik Lundh17741be2001-03-22 15:51:28 +0000691 if result is None:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000692 print '=== Fails on unicode pattern match', t
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000693
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000694 # Try the match with the search area limited to the extent
695 # of the match and see if it still succeeds. \B will
696 # break (because it won't match at the end or start of a
697 # string), so we'll ignore patterns that feature it.
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000698
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000699 if pattern[:2] != '\\B' and pattern[-2:] != '\\B' \
700 and result is not None:
701 obj = re.compile(pattern)
702 result = obj.search(s, result.start(0), result.end(0) + 1)
703 if result is None:
704 print '=== Failed on range-limited match', t
Fredrik Lundh1151a8c2000-08-08 16:47:42 +0000705
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000706 # Try the match with IGNORECASE enabled, and check that it
707 # still succeeds.
708 obj = re.compile(pattern, re.IGNORECASE)
709 result = obj.search(s)
Fred Drake132dce22000-12-12 23:11:42 +0000710 if result is None:
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000711 print '=== Fails on case-insensitive match', t
Guido van Rossumdfa67901997-12-08 17:12:06 +0000712
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000713 # Try the match with LOCALE enabled, and check that it
714 # still succeeds.
715 obj = re.compile(pattern, re.LOCALE)
716 result = obj.search(s)
717 if result is None:
718 print '=== Fails on locale-sensitive match', t
Guido van Rossumdfa67901997-12-08 17:12:06 +0000719
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000720 # Try the match with UNICODE locale enabled, and check
721 # that it still succeeds.
722 obj = re.compile(pattern, re.UNICODE)
723 result = obj.search(s)
724 if result is None:
725 print '=== Fails on unicode-sensitive match', t
Fredrik Lundh8e6d5712000-08-08 17:06:53 +0000726
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000727def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000728 run_unittest(ReTests)
Skip Montanaro1e703c62003-04-25 15:40:28 +0000729 run_re_tests()
Skip Montanaro8ed06da2003-04-24 19:43:18 +0000730
731if __name__ == "__main__":
732 test_main()