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

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

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 4 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
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 803 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 b'c:\xff') 814 b'c:\xff')
815 self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')), 815 self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')),
816 b's:cstr') 816 b's:cstr')
817 817
818 # Issue #19969 818 # Issue #19969
819 self.assertRaises(OverflowError, 819 self.assertRaises(OverflowError,
820 PyBytes_FromFormat, b'%c', c_int(-1)) 820 PyBytes_FromFormat, b'%c', c_int(-1))
821 self.assertRaises(OverflowError, 821 self.assertRaises(OverflowError,
822 PyBytes_FromFormat, b'%c', c_int(256)) 822 PyBytes_FromFormat, b'%c', c_int(256))
823 823
824
825 def test_bytes_blocking(self):
826 class Seq:
storchaka 2016/01/06 21:55:31 Why not use just list?
abarnert 2016/01/06 23:04:32 Good point. There's no direct fallback from __byte
827 def __getitem__(self, i):
828 if 0 <= i < 4:
829 return i
830 raise IndexError
831 class SeqBlocked(Seq):
832 __bytes__ = None
833 s, sb = Seq(), SeqBlocked()
834 self.assertEqual(bytes(s), b'\x00\x01\x02\x03')
835 self.assertRaises(TypeError, bytes, sb)
836
837 class BytesSubclass(bytes):
838 pass
839 class BytesSubclassBlocked(BytesSubclass):
storchaka 2016/01/06 21:55:31 Is it needed to inherit from bytes subclass, not j
abarnert 2016/01/06 23:04:32 Yeah, we don't need to test that bytes(BytesSubcla
abarnert 2016/01/07 21:57:37 Looking over the source code a bit more... I think
840 __bytes__ = None
841 bs, bsb = BytesSubclass(b'ab'), BytesSubclassBlocked(b'ab')
842 self.assertEqual(bytes(bs), b'ab')
843 self.assertRaises(TypeError, bytes, bsb)
844
845 class BytesLikeSubclass(bytearray):
846 pass
847 class BytesLikeSubclassBlocked(BytesLikeSubclass):
848 __bytes__ = None
849 bls, blsb = BytesLikeSubclass(b'ab'), BytesLikeSubclassBlocked(b'ab')
850 self.assertEqual(bytes(bls), b'ab')
851 self.assertRaises(TypeError, bytes, blsb)
824 852
825 class ByteArrayTest(BaseBytesTest, unittest.TestCase): 853 class ByteArrayTest(BaseBytesTest, unittest.TestCase):
826 type2test = bytearray 854 type2test = bytearray
827 855
828 def test_getitem_error(self): 856 def test_getitem_error(self):
829 msg = "bytearray indices must be integers or slices" 857 msg = "bytearray indices must be integers or slices"
830 with self.assertRaisesRegex(TypeError, msg): 858 with self.assertRaisesRegex(TypeError, msg):
831 bytearray(b'python')['a'] 859 bytearray(b'python')['a']
832 860
833 def test_setitem_error(self): 861 def test_setitem_error(self):
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
1584 self.assertEqual(x, b"abcd") 1612 self.assertEqual(x, b"abcd")
1585 1613
1586 1614
1587 class BytesSubclassTest(SubclassTest, unittest.TestCase): 1615 class BytesSubclassTest(SubclassTest, unittest.TestCase):
1588 type2test = bytes 1616 type2test = bytes
1589 subclass2test = BytesSubclass 1617 subclass2test = BytesSubclass
1590 1618
1591 1619
1592 if __name__ == "__main__": 1620 if __name__ == "__main__":
1593 unittest.main() 1621 unittest.main()
OLDNEW

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