memio.close()
self.assertRaises(ValueError, memio.getbuffer)
+ def test_getbuffer_empty(self):
+ memio = self.ioclass()
+ buf = memio.getbuffer()
+ self.assertEqual(bytes(buf), b"")
+ # Trying to change the size of the BytesIO while a buffer is exported
+ # raises a BufferError.
+ self.assertRaises(BufferError, memio.write, b'x')
+ buf2 = memio.getbuffer()
+ self.assertRaises(BufferError, memio.write, b'x')
+ buf.release()
+ self.assertRaises(BufferError, memio.write, b'x')
+ buf2.release()
+ memio.write(b'x')
+
def test_read1(self):
buf = self.buftype("1234567890")
self.assertEqual(self.ioclass(buf).read1(), buf)
static int
resize_buffer(bytesio *self, size_t size)
{
+ assert(self->buf != NULL);
+ assert(self->exports == 0);
+
/* Here, unsigned types are used to avoid dealing with signed integer
overflow, which is undefined in C. */
size_t alloc = PyBytes_GET_SIZE(self->buf);
- assert(self->buf != NULL);
-
/* For simplicity, stay in the range of the signed type. Anyway, Python
doesn't allow strings to be longer than this. */
if (size > PY_SSIZE_T_MAX)
"bytesiobuf_getbuffer: view==NULL argument is obsolete");
return -1;
}
- if (SHARED_BUF(b)) {
+ if (b->exports == 0 && SHARED_BUF(b)) {
if (unshare_buffer(b, b->string_size) < 0)
return -1;
}