Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(206750)

Delta Between Two Patch Sets: Lib/test/test_re.py

Issue 22578: Add addition attributes to re.error
Left Patch Set: Created 5 years, 2 months ago
Right Patch Set: Created 5 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/sre_parse.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \ 1 from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
2 cpython_only, captured_stdout 2 cpython_only, captured_stdout
3 import io 3 import io
4 import re 4 import re
5 from re import Scanner 5 from re import Scanner
6 import sre_compile 6 import sre_compile
7 import sre_constants 7 import sre_constants
8 import sys 8 import sys
9 import string 9 import string
10 import traceback 10 import traceback
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 pat = '(?:%s)(?(200)z|t)' % pat 218 pat = '(?:%s)(?(200)z|t)' % pat
219 self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5)) 219 self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
220 220
221 def test_symbolic_refs(self): 221 def test_symbolic_refs(self):
222 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx') 222 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
223 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx') 223 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
224 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx') 224 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
225 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx') 225 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
226 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<>', 'xx') 226 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<>', 'xx')
227 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx') 227 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
228 self.assertRaises(re.error, re.sub, '(?P<a>x)', r'\g<2>', 'xx')
229 self.assertRaises(re.error, re.sub, '(?P<a>x)', r'\2', 'xx')
228 self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx') 230 self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
229 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx') 231 self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\g<b>', 'xx'), '')
230 self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx') 232 self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\2', 'xx'), '')
231 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx') 233 self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx')
232 # New valid/invalid identifiers in Python 3 234 # New valid/invalid identifiers in Python 3
233 self.assertEqual(re.sub('(?P<ยต>x)', r'\g<ยต>', 'xx'), 'xx') 235 self.assertEqual(re.sub('(?P<ยต>x)', r'\g<ยต>', 'xx'), 'xx')
234 self.assertEqual(re.sub('(?P<๐”˜๐”ซ๐”ฆ๐” ๐”ฌ๐”ก๐”ข>x)', r'\g<๐”˜๐”ซ๐”ฆ๐” ๐”ฌ๐”ก๐”ข>', 'xx'), 'xx') 236 self.assertEqual(re.sub('(?P<๐”˜๐”ซ๐”ฆ๐” ๐”ฌ๐”ก๐”ข>x)', r'\g<๐”˜๐”ซ๐”ฆ๐” ๐”ฌ๐”ก๐”ข>', 'xx'), 'xx')
235 self.assertRaises(re.error, re.sub, '(?P<a>x)', r'\g<ยฉ>', 'xx') 237 self.assertRaises(re.error, re.sub, '(?P<a>x)', r'\g<ยฉ>', 'xx')
236 # Support > 100 groups. 238 # Support > 100 groups.
237 pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1)) 239 pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
238 self.assertEqual(re.sub(pat, '\g<200>', 'xc8yzxc8y'), 'c8zc8') 240 self.assertEqual(re.sub(pat, '\g<200>', 'xc8yzxc8y'), 'c8zc8')
239 241
240 def test_re_subn(self): 242 def test_re_subn(self):
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 def test_groupdict(self): 434 def test_groupdict(self):
433 self.assertEqual(re.match('(?P<first>first) (?P<second>second)', 435 self.assertEqual(re.match('(?P<first>first) (?P<second>second)',
434 'first second').groupdict(), 436 'first second').groupdict(),
435 {'first':'first', 'second':'second'}) 437 {'first':'first', 'second':'second'})
436 438
437 def test_expand(self): 439 def test_expand(self):
438 self.assertEqual(re.match("(?P<first>first) (?P<second>second)", 440 self.assertEqual(re.match("(?P<first>first) (?P<second>second)",
439 "first second") 441 "first second")
440 .expand(r"\2 \1 \g<second> \g<first>"), 442 .expand(r"\2 \1 \g<second> \g<first>"),
441 "second first second first") 443 "second first second first")
444 self.assertEqual(re.match("(?P<first>first)|(?P<second>second)",
445 "first")
446 .expand(r"\2 \g<second>"),
447 " ")
442 448
443 def test_repeat_minmax(self): 449 def test_repeat_minmax(self):
444 self.assertIsNone(re.match("^(\w){1}$", "abc")) 450 self.assertIsNone(re.match("^(\w){1}$", "abc"))
445 self.assertIsNone(re.match("^(\w){1}?$", "abc")) 451 self.assertIsNone(re.match("^(\w){1}?$", "abc"))
446 self.assertIsNone(re.match("^(\w){1,2}$", "abc")) 452 self.assertIsNone(re.match("^(\w){1,2}$", "abc"))
447 self.assertIsNone(re.match("^(\w){1,2}?$", "abc")) 453 self.assertIsNone(re.match("^(\w){1,2}?$", "abc"))
448 454
449 self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c") 455 self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
450 self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c") 456 self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
451 self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c") 457 self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
(...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 re.compile('(\u20ac))') 1281 re.compile('(\u20ac))')
1276 err = cm.exception 1282 err = cm.exception
1277 self.assertIsInstance(err.pattern, str) 1283 self.assertIsInstance(err.pattern, str)
1278 self.assertEqual(err.pattern, '(\u20ac))') 1284 self.assertEqual(err.pattern, '(\u20ac))')
1279 self.assertEqual(err.pos, 3) 1285 self.assertEqual(err.pos, 3)
1280 self.assertEqual(err.lineno, 1) 1286 self.assertEqual(err.lineno, 1)
1281 self.assertEqual(err.colno, 4) 1287 self.assertEqual(err.colno, 4)
1282 self.assertIn(err.msg, str(err)) 1288 self.assertIn(err.msg, str(err))
1283 self.assertIn(' at position 3', str(err)) 1289 self.assertIn(' at position 3', str(err))
1284 self.assertNotIn(' at position 3', err.msg) 1290 self.assertNotIn(' at position 3', err.msg)
1291 # Bytes pattern
1285 with self.assertRaises(re.error) as cm: 1292 with self.assertRaises(re.error) as cm:
1286 re.compile(b'(\xa4))') 1293 re.compile(b'(\xa4))')
1287 err = cm.exception 1294 err = cm.exception
1288 self.assertIsInstance(err.pattern, bytes) 1295 self.assertIsInstance(err.pattern, bytes)
1289 self.assertEqual(err.pattern, b'(\xa4))') 1296 self.assertEqual(err.pattern, b'(\xa4))')
1290 self.assertEqual(err.pos, 3) 1297 self.assertEqual(err.pos, 3)
1298 # Multiline pattern
1291 with self.assertRaises(re.error) as cm: 1299 with self.assertRaises(re.error) as cm:
1292 re.compile(""" 1300 re.compile("""
1293 ( 1301 (
1294 abc 1302 abc
1295 ) 1303 )
1296 ) 1304 )
1297 ( 1305 (
1298 """, re.VERBOSE) 1306 """, re.VERBOSE)
1299 err = cm.exception 1307 err = cm.exception
1300 self.assertEqual(err.pos, 77) 1308 self.assertEqual(err.pos, 77)
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1509 if result is None: 1517 if result is None:
1510 print('=== Fails on unicode-sensitive match', t) 1518 print('=== Fails on unicode-sensitive match', t)
1511 1519
1512 1520
1513 def test_main(): 1521 def test_main():
1514 run_unittest(__name__) 1522 run_unittest(__name__)
1515 run_re_tests() 1523 run_re_tests()
1516 1524
1517 if __name__ == "__main__": 1525 if __name__ == "__main__":
1518 test_main() 1526 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+