OLD | NEW |
1 import sys | 1 import sys |
2 sys.path = ['.'] + sys.path | 2 sys.path = ['.'] + sys.path |
3 | 3 |
4 from test.support import verbose, run_unittest, catch_warning | 4 from test.support import verbose, run_unittest, catch_warning |
5 import re | 5 import re |
6 from re import Scanner | 6 from re import Scanner |
7 import sys, os, traceback | 7 import sys, os, traceback |
8 from weakref import proxy | 8 from weakref import proxy |
9 | 9 |
10 # Misc tests from Tim Peters' re.doc | 10 # Misc tests from Tim Peters' re.doc |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 # Test for sub() on escaped characters | 76 # Test for sub() on escaped characters |
77 self.assertEqual(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n'), | 77 self.assertEqual(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n'), |
78 'abc\ndef\n') | 78 'abc\ndef\n') |
79 self.assertEqual(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n'), | 79 self.assertEqual(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n'), |
80 'abc\ndef\n') | 80 'abc\ndef\n') |
81 self.assertEqual(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n'), | 81 self.assertEqual(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n'), |
82 'abc\ndef\n') | 82 'abc\ndef\n') |
83 self.assertEqual(re.sub('\r\n', '\n', 'abc\r\ndef\r\n'), | 83 self.assertEqual(re.sub('\r\n', '\n', 'abc\r\ndef\r\n'), |
84 'abc\ndef\n') | 84 'abc\ndef\n') |
85 | 85 |
86 def test_bug_1140(self): | |
87 # re.sub(x, y, b'') should return b'', not '', and | |
88 # re.sub(x, y, '') should return '', not b''. | |
89 # Also: | |
90 # re.sub(x, y, str(x)) should return str(y), and | |
91 # re.sub(x, y, bytes(x)) should return | |
92 # str(y) if isinstance(y, str) else unicode(y). | |
93 for x in 'x', b'x': | |
94 for y in 'y', b'y': | |
95 z = re.sub(x, y, b'') | |
96 self.assertEqual(z, b'') | |
97 self.assertEqual(type(z), bytes) | |
98 # | |
99 z = re.sub(x, y, '') | |
100 self.assertEqual(z, '') | |
101 self.assertEqual(type(z), str) | |
102 | |
103 def test_bug_1661(self): | 86 def test_bug_1661(self): |
104 # Verify that flags do not get silently ignored with compiled patterns | 87 # Verify that flags do not get silently ignored with compiled patterns |
105 pattern = re.compile('.') | 88 pattern = re.compile('.') |
106 self.assertRaises(ValueError, re.match, pattern, 'A', re.I) | 89 self.assertRaises(ValueError, re.match, pattern, 'A', re.I) |
107 self.assertRaises(ValueError, re.search, pattern, 'A', re.I) | 90 self.assertRaises(ValueError, re.search, pattern, 'A', re.I) |
108 self.assertRaises(ValueError, re.findall, pattern, 'A', re.I) | 91 self.assertRaises(ValueError, re.findall, pattern, 'A', re.I) |
109 self.assertRaises(ValueError, re.compile, pattern, re.I) | 92 self.assertRaises(ValueError, re.compile, pattern, re.I) |
110 | 93 |
111 def test_sub_template_numeric_escape(self): | 94 def test_sub_template_numeric_escape(self): |
112 # bug 776311 and friends | 95 # bug 776311 and friends |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 self.assertNotEqual(re.match("^x{3}?$", "xxx"), None) | 303 self.assertNotEqual(re.match("^x{3}?$", "xxx"), None) |
321 self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None) | 304 self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None) |
322 self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None) | 305 self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None) |
323 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None) | 306 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None) |
324 | 307 |
325 self.assertEqual(re.match("^x{}$", "xxx"), None) | 308 self.assertEqual(re.match("^x{}$", "xxx"), None) |
326 self.assertNotEqual(re.match("^x{}$", "x{}"), None) | 309 self.assertNotEqual(re.match("^x{}$", "x{}"), None) |
327 | 310 |
328 def test_getattr(self): | 311 def test_getattr(self): |
329 self.assertEqual(re.compile("(?i)(a)(b)").pattern, "(?i)(a)(b)") | 312 self.assertEqual(re.compile("(?i)(a)(b)").pattern, "(?i)(a)(b)") |
330 self.assertEqual(re.compile("(?i)(a)(b)").flags, re.I) | 313 self.assertEqual(re.compile("(?i)(a)(b)").flags, re.I | re.U) |
331 self.assertEqual(re.compile("(?i)(a)(b)").groups, 2) | 314 self.assertEqual(re.compile("(?i)(a)(b)").groups, 2) |
332 self.assertEqual(re.compile("(?i)(a)(b)").groupindex, {}) | 315 self.assertEqual(re.compile("(?i)(a)(b)").groupindex, {}) |
333 self.assertEqual(re.compile("(?i)(?P<first>a)(?P<other>b)").groupindex, | 316 self.assertEqual(re.compile("(?i)(?P<first>a)(?P<other>b)").groupindex, |
334 {'first': 1, 'other': 2}) | 317 {'first': 1, 'other': 2}) |
335 | 318 |
336 self.assertEqual(re.match("(a)", "a").pos, 0) | 319 self.assertEqual(re.match("(a)", "a").pos, 0) |
337 self.assertEqual(re.match("(a)", "a").endpos, 1) | 320 self.assertEqual(re.match("(a)", "a").endpos, 1) |
338 self.assertEqual(re.match("(a)", "a").string, "a") | 321 self.assertEqual(re.match("(a)", "a").string, "a") |
339 self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) | 322 self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) |
340 self.assertNotEqual(re.match("(a)", "a").re, None) | 323 self.assertNotEqual(re.match("(a)", "a").re, None) |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 iter = re.finditer(r".*", "asdf") | 590 iter = re.finditer(r".*", "asdf") |
608 self.assertEqual(next(iter).span(), (0, 4)) | 591 self.assertEqual(next(iter).span(), (0, 4)) |
609 self.assertEqual(next(iter).span(), (4, 4)) | 592 self.assertEqual(next(iter).span(), (4, 4)) |
610 self.assertRaises(StopIteration, next, iter) | 593 self.assertRaises(StopIteration, next, iter) |
611 | 594 |
612 def test_empty_array(self): | 595 def test_empty_array(self): |
613 # SF buf 1647541 | 596 # SF buf 1647541 |
614 import array | 597 import array |
615 for typecode in 'bBuhHiIlLfd': | 598 for typecode in 'bBuhHiIlLfd': |
616 a = array.array(typecode) | 599 a = array.array(typecode) |
617 self.assertEqual(re.compile("bla").match(a), None) | 600 self.assertEqual(re.compile(b"bla").match(a), None) |
618 self.assertEqual(re.compile("").match(a).groups(), ()) | 601 self.assertEqual(re.compile(b"").match(a).groups(), ()) |
619 | 602 |
620 def test_inline_flags(self): | 603 def test_inline_flags(self): |
621 # Bug #1700 | 604 # Bug #1700 |
622 upper_char = chr(0x1ea0) # Latin Capital Letter A with Dot Bellow | 605 upper_char = chr(0x1ea0) # Latin Capital Letter A with Dot Bellow |
623 lower_char = chr(0x1ea1) # Latin Small Letter A with Dot Bellow | 606 lower_char = chr(0x1ea1) # Latin Small Letter A with Dot Bellow |
624 | 607 |
625 p = re.compile(upper_char, re.I | re.U) | 608 p = re.compile(upper_char, re.I | re.U) |
626 q = p.match(lower_char) | 609 q = p.match(lower_char) |
627 self.assertNotEqual(q, None) | 610 self.assertNotEqual(q, None) |
628 | 611 |
(...skipping 22 matching lines...) Expand all Loading... |
651 pattern = re.compile('$') | 634 pattern = re.compile('$') |
652 self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#') | 635 self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#') |
653 self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#') | 636 self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#') |
654 self.assertEqual(pattern.sub('#', '\n'), '#\n#') | 637 self.assertEqual(pattern.sub('#', '\n'), '#\n#') |
655 | 638 |
656 pattern = re.compile('$', re.MULTILINE) | 639 pattern = re.compile('$', re.MULTILINE) |
657 self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' ) | 640 self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' ) |
658 self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#') | 641 self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#') |
659 self.assertEqual(pattern.sub('#', '\n'), '#\n#') | 642 self.assertEqual(pattern.sub('#', '\n'), '#\n#') |
660 | 643 |
| 644 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 |
661 | 686 |
662 def run_re_tests(): | 687 def run_re_tests(): |
663 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR | 688 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR |
664 if verbose: | 689 if verbose: |
665 print('Running re_tests test suite') | 690 print('Running re_tests test suite') |
666 else: | 691 else: |
667 # To save time, only run the first and last 10 tests | 692 # To save time, only run the first and last 10 tests |
668 #tests = tests[:10] + tests[-10:] | 693 #tests = tests[:10] + tests[-10:] |
669 pass | 694 pass |
670 | 695 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 result = obj.search(s) | 810 result = obj.search(s) |
786 if result is None: | 811 if result is None: |
787 print('=== Fails on unicode-sensitive match', t) | 812 print('=== Fails on unicode-sensitive match', t) |
788 | 813 |
789 def test_main(): | 814 def test_main(): |
790 run_unittest(ReTests) | 815 run_unittest(ReTests) |
791 run_re_tests() | 816 run_re_tests() |
792 | 817 |
793 if __name__ == "__main__": | 818 if __name__ == "__main__": |
794 test_main() | 819 test_main() |
OLD | NEW |