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

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

Issue 25958: Implicit ABCs have no means of "anti-registration"
Left Patch Set: Created 4 years, 1 month ago
Right Patch Set: Created 3 years, 6 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:
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_bool.py ('k') | Lib/test/test_collections.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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 unified with string_tests.py (and 3 XXX This is a mess. Common tests should be unified with string_tests.py (and
4 the latter should be modernized). 4 the latter should be modernized).
5 """ 5 """
6 6
7 import os 7 import os
8 import re 8 import re
9 import sys 9 import sys
10 import copy 10 import copy
(...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 self.assertEqual(PyBytes_FromFormat(b'%'), b'%') 835 self.assertEqual(PyBytes_FromFormat(b'%'), b'%')
836 self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)), 836 self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)),
837 b'x=2 y=%') 837 b'x=2 y=%')
838 838
839 # Issue #19969: %c must raise OverflowError for values 839 # Issue #19969: %c must raise OverflowError for values
840 # not in the range [0; 255] 840 # not in the range [0; 255]
841 self.assertRaises(OverflowError, 841 self.assertRaises(OverflowError,
842 PyBytes_FromFormat, b'%c', c_int(-1)) 842 PyBytes_FromFormat, b'%c', c_int(-1))
843 self.assertRaises(OverflowError, 843 self.assertRaises(OverflowError,
844 PyBytes_FromFormat, b'%c', c_int(256)) 844 PyBytes_FromFormat, b'%c', c_int(256))
845
846 def test_bytes_blocking(self):
847 class IterationBlocked(list):
848 __bytes__ = None
849 i = [0, 1, 2, 3]
850 self.assertEqual(bytes(i), b'\x00\x01\x02\x03')
851 self.assertRaises(TypeError, bytes, IterationBlocked(i))
852
853 # At least in CPython, because bytes.__new__ and the C API
854 # PyBytes_FromObject have different fallback rules, integer
855 # fallback is handled specially, so test separately.
856 class IntBlocked(int):
857 __bytes__ = None
858 self.assertEqual(bytes(3), b'\0\0\0')
859 self.assertRaises(TypeError, bytes, IntBlocked(3))
860
861 # While there is no separately-defined rule for handling bytes
862 # subclasses differently from other buffer-interface classes,
863 # an implementation may well special-case them (as CPython 2.x
864 # str did), so test them separately.
865 class BytesSubclassBlocked(bytes):
866 __bytes__ = None
867 self.assertEqual(bytes(b'ab'), b'ab')
868 self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab'))
869
870 class BufferBlocked(bytearray):
871 __bytes__ = None
872 ba, bb = bytearray(b'ab'), BufferBlocked(b'ab')
873 self.assertEqual(bytes(ba), b'ab')
874 self.assertRaises(TypeError, bytes, bb)
845 875
846 876
847 class ByteArrayTest(BaseBytesTest, unittest.TestCase): 877 class ByteArrayTest(BaseBytesTest, unittest.TestCase):
848 type2test = bytearray 878 type2test = bytearray
849 879
850 def test_getitem_error(self): 880 def test_getitem_error(self):
851 msg = "bytearray indices must be integers or slices" 881 msg = "bytearray indices must be integers or slices"
852 with self.assertRaisesRegex(TypeError, msg): 882 with self.assertRaisesRegex(TypeError, msg):
853 bytearray(b'python')['a'] 883 bytearray(b'python')['a']
854 884
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
1652 self.assertEqual(x, b"abcd") 1682 self.assertEqual(x, b"abcd")
1653 1683
1654 1684
1655 class BytesSubclassTest(SubclassTest, unittest.TestCase): 1685 class BytesSubclassTest(SubclassTest, unittest.TestCase):
1656 basetype = bytes 1686 basetype = bytes
1657 type2test = BytesSubclass 1687 type2test = BytesSubclass
1658 1688
1659 1689
1660 if __name__ == "__main__": 1690 if __name__ == "__main__":
1661 unittest.main() 1691 unittest.main()
LEFTRIGHT

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