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

Side by Side Diff: Lib/test/test_bytes.py

Issue 11828: startswith and endswith don't accept None as slice index
Patch Set: Created 2 years, 1 month 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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/string_tests.py ('k') | Objects/bytearrayobject.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Unit tests for the bytes and bytearray types. 1 """Unit tests for the bytes and bytearray types.
2 2
3 XXX This is a mess. Common tests should be moved to buffer_tests.py, 3 XXX This is a mess. Common tests should be moved to buffer_tests.py,
4 which itself ought to be unified with string_tests.py (and the latter 4 which itself ought to be unified with string_tests.py (and the latter
5 should be modernized). 5 should be modernized).
6 """ 6 """
7 7
8 import os 8 import os
9 import re 9 import re
10 import sys 10 import sys
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], 451 self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
452 [0, 65, 127, 128, 255]) 452 [0, 65, 127, 128, 255])
453 453
454 def test_maketrans(self): 454 def test_maketrans(self):
455 transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\ 016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\' ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuv wxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220 \221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244 \245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270 \271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314 \315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340 \341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364 \365\366\367\370\371\372\373\374\375\376\377' 455 transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\ 016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\' ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuv wxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220 \221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244 \245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270 \271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314 \315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340 \341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364 \365\366\367\370\371\372\373\374\375\376\377'
456 self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable) 456 self.assertEqual(self.type2test.maketrans(b'abc', b'xyz'), transtable)
457 transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\ 016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\' ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuv wxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220 \221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244 \245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270 \271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314 \315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340 \341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364 \365\366\367\370\371\372\373\374xyz' 457 transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\ 016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\' ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuv wxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220 \221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244 \245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270 \271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314 \315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340 \341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364 \365\366\367\370\371\372\373\374xyz'
458 self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), tran stable) 458 self.assertEqual(self.type2test.maketrans(b'\375\376\377', b'xyz'), tran stable)
459 self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq') 459 self.assertRaises(ValueError, self.type2test.maketrans, b'abc', b'xyzq')
460 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def') 460 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def')
461
462 def test_none_arguments(self):
463 # issue 11828
464 b = self.type2test(b'hello')
465 l = self.type2test(b'l')
466 h = self.type2test(b'h')
467 x = self.type2test(b'x')
468 o = self.type2test(b'o')
469
470 self.assertEqual(2, b.find(l, None))
471 self.assertEqual(3, b.find(l, -2, None))
472 self.assertEqual(2, b.find(l, None, -2))
473 self.assertEqual(0, b.find(h, None, None))
474
475 self.assertEqual(3, b.rfind(l, None))
476 self.assertEqual(3, b.rfind(l, -2, None))
477 self.assertEqual(2, b.rfind(l, None, -2))
478 self.assertEqual(0, b.rfind(h, None, None))
479
480 self.assertEqual(2, b.index(l, None))
481 self.assertEqual(3, b.index(l, -2, None))
482 self.assertEqual(2, b.index(l, None, -2))
483 self.assertEqual(0, b.index(h, None, None))
484
485 self.assertEqual(3, b.rindex(l, None))
486 self.assertEqual(3, b.rindex(l, -2, None))
487 self.assertEqual(2, b.rindex(l, None, -2))
488 self.assertEqual(0, b.rindex(h, None, None))
489
490 self.assertEqual(2, b.count(l, None))
491 self.assertEqual(1, b.count(l, -2, None))
492 self.assertEqual(1, b.count(l, None, -2))
493 self.assertEqual(0, b.count(x, None, None))
494
495 self.assertEqual(True, b.endswith(o, None))
496 self.assertEqual(True, b.endswith(o, -2, None))
497 self.assertEqual(True, b.endswith(l, None, -2))
498 self.assertEqual(False, b.endswith(x, None, None))
499
500 self.assertEqual(True, b.startswith(h, None))
501 self.assertEqual(True, b.startswith(l, -2, None))
502 self.assertEqual(True, b.startswith(h, None, -2))
503 self.assertEqual(False, b.startswith(x, None, None))
504
505 def test_find_etc_raise_correct_error_messages(self):
506 # issue 11828
507 b = self.type2test(b'hello')
508 x = self.type2test(b'x')
509 self.assertRaisesRegexp(TypeError, r'\bfind\b', b.find,
510 x, None, None, None)
511 self.assertRaisesRegexp(TypeError, r'\brfind\b', b.rfind,
512 x, None, None, None)
513 self.assertRaisesRegexp(TypeError, r'\bindex\b', b.index,
514 x, None, None, None)
515 self.assertRaisesRegexp(TypeError, r'\brindex\b', b.rindex,
516 x, None, None, None)
517 self.assertRaisesRegexp(TypeError, r'\bcount\b', b.count,
518 x, None, None, None)
519 self.assertRaisesRegexp(TypeError, r'\bstartswith\b', b.startswith,
520 x, None, None, None)
521 self.assertRaisesRegexp(TypeError, r'\bendswith\b', b.endswith,
522 x, None, None, None)
461 523
462 524
463 class BytesTest(BaseBytesTest): 525 class BytesTest(BaseBytesTest):
464 type2test = bytes 526 type2test = bytes
465 527
466 def test_buffer_is_readonly(self): 528 def test_buffer_is_readonly(self):
467 fd = os.dup(sys.stdin.fileno()) 529 fd = os.dup(sys.stdin.fileno())
468 with open(fd, "rb", buffering=0) as f: 530 with open(fd, "rb", buffering=0) as f:
469 self.assertRaises(TypeError, f.readinto, b"") 531 self.assertRaises(TypeError, f.readinto, b"")
470 532
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after
1082 1144
1083 1145
1084 def test_main(): 1146 def test_main():
1085 test.support.run_unittest( 1147 test.support.run_unittest(
1086 BytesTest, AssortedBytesTest, BytesAsStringTest, 1148 BytesTest, AssortedBytesTest, BytesAsStringTest,
1087 ByteArrayTest, ByteArrayAsStringTest, ByteArraySubclassTest, 1149 ByteArrayTest, ByteArrayAsStringTest, ByteArraySubclassTest,
1088 BytearrayPEP3137Test) 1150 BytearrayPEP3137Test)
1089 1151
1090 if __name__ == "__main__": 1152 if __name__ == "__main__":
1091 test_main() 1153 test_main()
OLDNEW
« no previous file with comments | « Lib/test/string_tests.py ('k') | Objects/bytearrayobject.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7