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

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

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 4 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:
View unified diff | Download patch
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 898 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)), 909 self.assertEqual(PyBytes_FromFormat(b'x=%i y=%', c_int(2), c_int(3)),
910 b'x=2 y=%') 910 b'x=2 y=%')
911 911
912 # Issue #19969: %c must raise OverflowError for values 912 # Issue #19969: %c must raise OverflowError for values
913 # not in the range [0; 255] 913 # not in the range [0; 255]
914 self.assertRaises(OverflowError, 914 self.assertRaises(OverflowError,
915 PyBytes_FromFormat, b'%c', c_int(-1)) 915 PyBytes_FromFormat, b'%c', c_int(-1))
916 self.assertRaises(OverflowError, 916 self.assertRaises(OverflowError,
917 PyBytes_FromFormat, b'%c', c_int(256)) 917 PyBytes_FromFormat, b'%c', c_int(256))
918 918
919
920 def test_bytes_blocking(self):
921 class IterationBlocked(list):
922 __bytes__ = None
923 i = [0, 1, 2, 3]
924 self.assertEqual(bytes(i), b'\x00\x01\x02\x03')
925 self.assertRaises(TypeError, bytes, IterationBlocked(i))
926
927 # At least in CPython, because bytes.__new__ and the C API
928 # PyBytes_FromObject have different fallback rules, integer
929 # fallback is handled specially, so test separately.
930 class IntBlocked(int):
931 __bytes__ = None
932 self.assertEqual(bytes(3), b'\0\0\0')
933 self.assertRaises(TypeError, bytes, IntBlocked(3))
934
935 # While there is no separately-defined rule for handling bytes
936 # subclasses differently from other buffer-interface classes,
937 # an implementation may well special-case them (as CPython 2.x
938 # str did), so test them separately.
939 class BytesSubclassBlocked(bytes):
940 __bytes__ = None
941 self.assertEqual(bytes(b'ab'), b'ab')
942 self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab'))
943
944 class BufferBlocked(bytearray):
945 __bytes__ = None
946 ba, bb = bytearray(b'ab'), BufferBlocked(b'ab')
947 self.assertEqual(bytes(ba), b'ab')
948 self.assertRaises(TypeError, bytes, bb)
919 949
920 class ByteArrayTest(BaseBytesTest, unittest.TestCase): 950 class ByteArrayTest(BaseBytesTest, unittest.TestCase):
921 type2test = bytearray 951 type2test = bytearray
922 952
923 def test_getitem_error(self): 953 def test_getitem_error(self):
924 msg = "bytearray indices must be integers or slices" 954 msg = "bytearray indices must be integers or slices"
925 with self.assertRaisesRegex(TypeError, msg): 955 with self.assertRaisesRegex(TypeError, msg):
926 bytearray(b'python')['a'] 956 bytearray(b'python')['a']
927 957
928 def test_setitem_error(self): 958 def test_setitem_error(self):
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
1679 self.assertEqual(x, b"abcd") 1709 self.assertEqual(x, b"abcd")
1680 1710
1681 1711
1682 class BytesSubclassTest(SubclassTest, unittest.TestCase): 1712 class BytesSubclassTest(SubclassTest, unittest.TestCase):
1683 type2test = bytes 1713 type2test = bytes
1684 subclass2test = BytesSubclass 1714 subclass2test = BytesSubclass
1685 1715
1686 1716
1687 if __name__ == "__main__": 1717 if __name__ == "__main__":
1688 unittest.main() 1718 unittest.main()
OLDNEW

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