LEFT | RIGHT |
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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None) | 302 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None) |
303 self.assertNotEqual(re.match("^x{3}?$", "xxx"), None) | 303 self.assertNotEqual(re.match("^x{3}?$", "xxx"), None) |
304 self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None) | 304 self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None) |
305 self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None) | 305 self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None) |
306 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None) | 306 self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None) |
307 | 307 |
308 self.assertEqual(re.match("^x{}$", "xxx"), None) | 308 self.assertEqual(re.match("^x{}$", "xxx"), None) |
309 self.assertNotEqual(re.match("^x{}$", "x{}"), None) | 309 self.assertNotEqual(re.match("^x{}$", "x{}"), None) |
310 | 310 |
311 def test_getattr(self): | 311 def test_getattr(self): |
| 312 self.assertEqual(re.compile("(?i)(a)(b)").pattern, "(?i)(a)(b)") |
| 313 self.assertEqual(re.compile("(?i)(a)(b)").flags, re.I | re.U) |
| 314 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 |
312 self.assertEqual(re.match("(a)", "a").pos, 0) | 319 self.assertEqual(re.match("(a)", "a").pos, 0) |
313 self.assertEqual(re.match("(a)", "a").endpos, 1) | 320 self.assertEqual(re.match("(a)", "a").endpos, 1) |
314 self.assertEqual(re.match("(a)", "a").string, "a") | 321 self.assertEqual(re.match("(a)", "a").string, "a") |
315 self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) | 322 self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) |
316 self.assertNotEqual(re.match("(a)", "a").re, None) | 323 self.assertNotEqual(re.match("(a)", "a").re, None) |
317 | 324 |
318 def test_special_escapes(self): | 325 def test_special_escapes(self): |
319 self.assertEqual(re.search(r"\b(b.)\b", | 326 self.assertEqual(re.search(r"\b(b.)\b", |
320 "abcd abc bcd bx").group(1), "bx") | 327 "abcd abc bcd bx").group(1), "bx") |
321 self.assertEqual(re.search(r"\B(b.)\B", | 328 self.assertEqual(re.search(r"\B(b.)\B", |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 bpat = re.compile(b'.') | 647 bpat = re.compile(b'.') |
641 self.assertRaises(TypeError, pat.match, b'b') | 648 self.assertRaises(TypeError, pat.match, b'b') |
642 self.assertRaises(TypeError, bpat.match, 'b') | 649 self.assertRaises(TypeError, bpat.match, 'b') |
643 self.assertRaises(TypeError, pat.sub, b'b', 'c') | 650 self.assertRaises(TypeError, pat.sub, b'b', 'c') |
644 self.assertRaises(TypeError, pat.sub, 'b', b'c') | 651 self.assertRaises(TypeError, pat.sub, 'b', b'c') |
645 self.assertRaises(TypeError, pat.sub, b'b', b'c') | 652 self.assertRaises(TypeError, pat.sub, b'b', b'c') |
646 self.assertRaises(TypeError, bpat.sub, b'b', 'c') | 653 self.assertRaises(TypeError, bpat.sub, b'b', 'c') |
647 self.assertRaises(TypeError, bpat.sub, 'b', b'c') | 654 self.assertRaises(TypeError, bpat.sub, 'b', b'c') |
648 self.assertRaises(TypeError, bpat.sub, 'b', 'c') | 655 self.assertRaises(TypeError, bpat.sub, 'b', 'c') |
649 | 656 |
650 def test_unicode_flag(self): | 657 def test_ascii_and_unicode_flag(self): |
651 # String patterns | 658 # String patterns |
652 for flags in (0, re.UNICODE): | 659 for flags in (0, re.UNICODE): |
653 pat = re.compile('\xc0', flags | re.IGNORECASE) | 660 pat = re.compile('\xc0', flags | re.IGNORECASE) |
654 self.assertNotEqual(pat.match('\xe0'), None) | 661 self.assertNotEqual(pat.match('\xe0'), None) |
655 pat = re.compile('\w', flags) | 662 pat = re.compile('\w', flags) |
656 self.assertNotEqual(pat.match('\xe0'), None) | 663 self.assertNotEqual(pat.match('\xe0'), None) |
657 pat = re.compile('\xc0', re.ASCII | re.IGNORECASE) | 664 pat = re.compile('\xc0', re.ASCII | re.IGNORECASE) |
658 self.assertEqual(pat.match('\xe0'), None) | 665 self.assertEqual(pat.match('\xe0'), None) |
| 666 pat = re.compile('(?a)\xc0', re.IGNORECASE) |
| 667 self.assertEqual(pat.match('\xe0'), None) |
659 pat = re.compile('\w', re.ASCII) | 668 pat = re.compile('\w', re.ASCII) |
| 669 self.assertEqual(pat.match('\xe0'), None) |
| 670 pat = re.compile('(?a)\w') |
660 self.assertEqual(pat.match('\xe0'), None) | 671 self.assertEqual(pat.match('\xe0'), None) |
661 # Bytes patterns | 672 # Bytes patterns |
662 for flags in (0, re.ASCII): | 673 for flags in (0, re.ASCII): |
663 pat = re.compile(b'\xc0', re.IGNORECASE) | 674 pat = re.compile(b'\xc0', re.IGNORECASE) |
664 self.assertEqual(pat.match(b'\xe0'), None) | 675 self.assertEqual(pat.match(b'\xe0'), None) |
665 pat = re.compile(b'\w') | 676 pat = re.compile(b'\w') |
666 self.assertEqual(pat.match(b'\xe0'), None) | 677 self.assertEqual(pat.match(b'\xe0'), None) |
667 # Incompatibilities | 678 # Incompatibilities |
668 self.assertRaises(ValueError, re.compile, b'\w', re.UNICODE) | 679 self.assertRaises(ValueError, re.compile, b'\w', re.UNICODE) |
669 self.assertRaises(ValueError, re.compile, b'(?u)\w') | 680 self.assertRaises(ValueError, re.compile, b'(?u)\w') |
670 self.assertRaises(ValueError, re.compile, '\w', | 681 self.assertRaises(ValueError, re.compile, '\w', re.UNICODE | re.ASCII) |
671 re.UNICODE | re.ASCII) | |
672 self.assertRaises(ValueError, re.compile, '(?u)\w', 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') |
673 | 685 |
674 | 686 |
675 def run_re_tests(): | 687 def run_re_tests(): |
676 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR | 688 from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR |
677 if verbose: | 689 if verbose: |
678 print('Running re_tests test suite') | 690 print('Running re_tests test suite') |
679 else: | 691 else: |
680 # To save time, only run the first and last 10 tests | 692 # To save time, only run the first and last 10 tests |
681 #tests = tests[:10] + tests[-10:] | 693 #tests = tests[:10] + tests[-10:] |
682 pass | 694 pass |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 result = obj.search(s) | 810 result = obj.search(s) |
799 if result is None: | 811 if result is None: |
800 print('=== Fails on unicode-sensitive match', t) | 812 print('=== Fails on unicode-sensitive match', t) |
801 | 813 |
802 def test_main(): | 814 def test_main(): |
803 run_unittest(ReTests) | 815 run_unittest(ReTests) |
804 run_re_tests() | 816 run_re_tests() |
805 | 817 |
806 if __name__ == "__main__": | 818 if __name__ == "__main__": |
807 test_main() | 819 test_main() |
LEFT | RIGHT |