mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
#2538: bytes objects can only provide read-only buffers
This commit is contained in:
parent
07431a302a
commit
2f89aa6785
4 changed files with 13 additions and 8 deletions
|
@ -453,6 +453,11 @@ class BaseBytesTest(unittest.TestCase):
|
||||||
class BytesTest(BaseBytesTest):
|
class BytesTest(BaseBytesTest):
|
||||||
type2test = bytes
|
type2test = bytes
|
||||||
|
|
||||||
|
def test_buffer_is_readonly(self):
|
||||||
|
with open(sys.stdin.fileno(), "rb", buffering=0) as f:
|
||||||
|
self.assertRaises(TypeError, f.readinto, b"")
|
||||||
|
|
||||||
|
|
||||||
class ByteArrayTest(BaseBytesTest):
|
class ByteArrayTest(BaseBytesTest):
|
||||||
type2test = bytearray
|
type2test = bytearray
|
||||||
|
|
||||||
|
|
|
@ -1112,7 +1112,7 @@ class BufferIOTest(SocketConnectedTest):
|
||||||
SocketConnectedTest.__init__(self, methodName=methodName)
|
SocketConnectedTest.__init__(self, methodName=methodName)
|
||||||
|
|
||||||
def testRecvInto(self):
|
def testRecvInto(self):
|
||||||
buf = b" "*1024
|
buf = bytearray(1024)
|
||||||
nbytes = self.cli_conn.recv_into(buf)
|
nbytes = self.cli_conn.recv_into(buf)
|
||||||
self.assertEqual(nbytes, len(MSG))
|
self.assertEqual(nbytes, len(MSG))
|
||||||
msg = buf[:len(MSG)]
|
msg = buf[:len(MSG)]
|
||||||
|
@ -1123,7 +1123,7 @@ class BufferIOTest(SocketConnectedTest):
|
||||||
self.serv_conn.send(buf)
|
self.serv_conn.send(buf)
|
||||||
|
|
||||||
def testRecvFromInto(self):
|
def testRecvFromInto(self):
|
||||||
buf = b" "*1024
|
buf = bytearray(1024)
|
||||||
nbytes, addr = self.cli_conn.recvfrom_into(buf)
|
nbytes, addr = self.cli_conn.recvfrom_into(buf)
|
||||||
self.assertEqual(nbytes, len(MSG))
|
self.assertEqual(nbytes, len(MSG))
|
||||||
msg = buf[:len(MSG)]
|
msg = buf[:len(MSG)]
|
||||||
|
|
|
@ -965,7 +965,7 @@ static int
|
||||||
string_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags)
|
string_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags)
|
||||||
{
|
{
|
||||||
return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_SIZE(self),
|
return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_SIZE(self),
|
||||||
0, flags);
|
1, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PySequenceMethods string_as_sequence = {
|
static PySequenceMethods string_as_sequence = {
|
||||||
|
|
|
@ -56,7 +56,7 @@ PyMemoryView_FromObject(PyObject *base)
|
||||||
if (mview == NULL) return NULL;
|
if (mview == NULL) return NULL;
|
||||||
|
|
||||||
mview->base = NULL;
|
mview->base = NULL;
|
||||||
if (PyObject_GetBuffer(base, &(mview->view), PyBUF_FULL) < 0) {
|
if (PyObject_GetBuffer(base, &(mview->view), PyBUF_FULL_RO) < 0) {
|
||||||
Py_DECREF(mview);
|
Py_DECREF(mview);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue