diff options
Diffstat (limited to 'Lib/test/test_bytes.py')
| -rw-r--r-- | Lib/test/test_bytes.py | 64 | 
1 files changed, 64 insertions, 0 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index e15807e..a9f64a0 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -461,6 +461,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 +744,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 +803,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()) @@ -974,6 +1012,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 @@ -1164,6 +1224,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):      #  | 
