diff options
Diffstat (limited to 'Lib/test/test_bytes.py')
| -rw-r--r-- | Lib/test/test_bytes.py | 113 | 
1 files changed, 100 insertions, 13 deletions
| diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index b00573f..53a80f4 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -301,6 +301,14 @@ class BaseBytesTest:          self.assertRaises(ValueError, self.type2test.fromhex, '\x00')          self.assertRaises(ValueError, self.type2test.fromhex, '12   \x00   34') +    def test_hex(self): +        self.assertRaises(TypeError, self.type2test.hex) +        self.assertRaises(TypeError, self.type2test.hex, 1) +        self.assertEqual(self.type2test(b"").hex(), "") +        self.assertEqual(bytearray([0x1a, 0x2b, 0x30]).hex(), '1a2b30') +        self.assertEqual(self.type2test(b"\x1a\x2b\x30").hex(), '1a2b30') +        self.assertEqual(memoryview(b"\x1a\x2b\x30").hex(), '1a2b30') +      def test_join(self):          self.assertEqual(self.type2test(b"").join([]), b"")          self.assertEqual(self.type2test(b"").join([b""]), b"") @@ -461,6 +469,28 @@ class BaseBytesTest:          self.assertEqual(b.rindex(i, 3, 9), 7)          self.assertRaises(ValueError, b.rindex, w, 1, 3) +    def test_mod(self): +        b = b'hello, %b!' +        orig = b +        b = b % b'world' +        self.assertEqual(b, b'hello, world!') +        self.assertEqual(orig, b'hello, %b!') +        self.assertFalse(b is orig) +        b = b'%s / 100 = %d%%' +        a = b % (b'seventy-nine', 79) +        self.assertEqual(a, b'seventy-nine / 100 = 79%') + +    def test_imod(self): +        b = b'hello, %b!' +        orig = b +        b %= b'world' +        self.assertEqual(b, b'hello, world!') +        self.assertEqual(orig, b'hello, %b!') +        self.assertFalse(b is orig) +        b = b'%s / 100 = %d%%' +        b %= (b'seventy-nine', 79) +        self.assertEqual(b, b'seventy-nine / 100 = 79%') +      def test_replace(self):          b = self.type2test(b'mississippi')          self.assertEqual(b.replace(b'i', b'a'), b'massassappa') @@ -722,6 +752,11 @@ class BaseBytesTest:  class BytesTest(BaseBytesTest, unittest.TestCase):      type2test = bytes +    def test_getitem_error(self): +        msg = "byte indices must be integers or slices" +        with self.assertRaisesRegex(TypeError, msg): +            b'python'['a'] +      def test_buffer_is_readonly(self):          fd = os.open(__file__, os.O_RDONLY)          with open(fd, "rb", buffering=0) as f: @@ -776,6 +811,17 @@ class BytesTest(BaseBytesTest, unittest.TestCase):  class ByteArrayTest(BaseBytesTest, unittest.TestCase):      type2test = bytearray +    def test_getitem_error(self): +        msg = "bytearray indices must be integers or slices" +        with self.assertRaisesRegex(TypeError, msg): +            bytearray(b'python')['a'] + +    def test_setitem_error(self): +        msg = "bytearray indices must be integers or slices" +        with self.assertRaisesRegex(TypeError, msg): +            b = bytearray(b'python') +            b['a'] = "python" +      def test_nohash(self):          self.assertRaises(TypeError, hash, bytearray()) @@ -990,6 +1036,28 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):          b[8:] = b          self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) +    def test_mod(self): +        b = bytearray(b'hello, %b!') +        orig = b +        b = b % b'world' +        self.assertEqual(b, b'hello, world!') +        self.assertEqual(orig, bytearray(b'hello, %b!')) +        self.assertFalse(b is orig) +        b = bytearray(b'%s / 100 = %d%%') +        a = b % (b'seventy-nine', 79) +        self.assertEqual(a, bytearray(b'seventy-nine / 100 = 79%')) + +    def test_imod(self): +        b = bytearray(b'hello, %b!') +        orig = b +        b %= b'world' +        self.assertEqual(b, b'hello, world!') +        self.assertEqual(orig, bytearray(b'hello, %b!')) +        self.assertFalse(b is orig) +        b = bytearray(b'%s / 100 = %d%%') +        b %= (b'seventy-nine', 79) +        self.assertEqual(b, bytearray(b'seventy-nine / 100 = 79%')) +      def test_iconcat(self):          b = bytearray(b"abc")          b1 = b @@ -1197,6 +1265,10 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):          self.assertRaises(BufferError, delslice)          self.assertEqual(b, orig) +    @test.support.cpython_only +    def test_obsolete_write_lock(self): +        from _testcapi import getbuffer_with_null_view +        self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())  class AssortedBytesTest(unittest.TestCase):      # @@ -1307,20 +1379,35 @@ class AssortedBytesTest(unittest.TestCase):          b = bytearray()          self.assertFalse(b.replace(b'', b'') is b) +    @unittest.skipUnless(sys.flags.bytes_warning, +                         "BytesWarning is needed for this test: use -bb option")      def test_compare(self): -        if sys.flags.bytes_warning: -            def bytes_warning(): -                return test.support.check_warnings(('', BytesWarning)) -            with bytes_warning(): -                b'' == '' -            with bytes_warning(): -                b'' != '' -            with bytes_warning(): -                bytearray(b'') == '' -            with bytes_warning(): -                bytearray(b'') != '' -        else: -            self.skipTest("BytesWarning is needed for this test: use -bb option") +        def bytes_warning(): +            return test.support.check_warnings(('', BytesWarning)) +        with bytes_warning(): +            b'' == '' +        with bytes_warning(): +            '' == b'' +        with bytes_warning(): +            b'' != '' +        with bytes_warning(): +            '' != b'' +        with bytes_warning(): +            bytearray(b'') == '' +        with bytes_warning(): +            '' == bytearray(b'') +        with bytes_warning(): +            bytearray(b'') != '' +        with bytes_warning(): +            '' != bytearray(b'') +        with bytes_warning(): +            b'\0' == 0 +        with bytes_warning(): +            0 == b'\0' +        with bytes_warning(): +            b'\0' != 0 +        with bytes_warning(): +            0 != b'\0'      # Optimizations:      # __iter__? (optimization) | 
