diff options
Diffstat (limited to 'Lib/test/test_bytes.py')
-rw-r--r-- | Lib/test/test_bytes.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 24812a5..b1427b5 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -769,6 +769,37 @@ class ByteArrayTest(BaseBytesTest): self.assertEqual(b, b"") self.assertEqual(c, b"") + def test_resize_forbidden(self): + # #4509: can't resize a bytearray when there are buffer exports, even + # if it wouldn't reallocate the underlying buffer. + # Furthermore, no destructive changes to the buffer may be applied + # before raising the error. + b = bytearray(range(10)) + v = memoryview(b) + def resize(n): + b[1:-1] = range(n + 1, 2*n - 1) + resize(10) + orig = b[:] + self.assertRaises(BufferError, resize, 11) + self.assertEquals(b, orig) + self.assertRaises(BufferError, resize, 9) + self.assertEquals(b, orig) + self.assertRaises(BufferError, resize, 0) + self.assertEquals(b, orig) + # Other operations implying resize + self.assertRaises(BufferError, b.pop, 0) + self.assertEquals(b, orig) + self.assertRaises(BufferError, b.remove, b[1]) + self.assertEquals(b, orig) + def delitem(): + del b[1] + self.assertRaises(BufferError, delitem) + self.assertEquals(b, orig) + # deleting a non-contiguous slice + def delslice(): + b[1:-1:2] = b"" + self.assertRaises(BufferError, delslice) + self.assertEquals(b, orig) class AssortedBytesTest(unittest.TestCase): # |