diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2008-08-02 21:02:48 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-08-02 21:02:48 (GMT) |
commit | 2f89aa678549eae1557247a36ed9194af9a4f7b7 (patch) | |
tree | b8de431ef79fb42ca92d8601977a8ab5ac533ca8 | |
parent | 07431a302a3698bec8b9eb9debc9f630479141e3 (diff) | |
download | cpython-2f89aa678549eae1557247a36ed9194af9a4f7b7.zip cpython-2f89aa678549eae1557247a36ed9194af9a4f7b7.tar.gz cpython-2f89aa678549eae1557247a36ed9194af9a4f7b7.tar.bz2 |
#2538: bytes objects can only provide read-only buffers
-rw-r--r-- | Lib/test/test_bytes.py | 5 | ||||
-rw-r--r-- | Lib/test/test_socket.py | 4 | ||||
-rw-r--r-- | Objects/bytesobject.c | 2 | ||||
-rw-r--r-- | Objects/memoryobject.c | 10 |
4 files changed, 13 insertions, 8 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 5bda935..230dbf4 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -453,6 +453,11 @@ class BaseBytesTest(unittest.TestCase): class BytesTest(BaseBytesTest): 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): type2test = bytearray diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 222c42c..9d753b7 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1112,7 +1112,7 @@ class BufferIOTest(SocketConnectedTest): SocketConnectedTest.__init__(self, methodName=methodName) def testRecvInto(self): - buf = b" "*1024 + buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] @@ -1123,7 +1123,7 @@ class BufferIOTest(SocketConnectedTest): self.serv_conn.send(buf) def testRecvFromInto(self): - buf = b" "*1024 + buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 471d09c..eeae0ff 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -965,7 +965,7 @@ static int string_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) { return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_SIZE(self), - 0, flags); + 1, flags); } static PySequenceMethods string_as_sequence = { diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 78ada17..79d7db1 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -56,7 +56,7 @@ PyMemoryView_FromObject(PyObject *base) if (mview == NULL) return 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); return NULL; } @@ -204,9 +204,9 @@ _indirect_copy_nd(char *dest, Py_buffer *view, char fort) a contiguous buffer if it is not. The view will point to the shadow buffer which can be written to and then will be copied back into the other buffer when the memory - view is de-allocated. While the shadow buffer is - being used, it will have an exclusive write lock on - the original buffer. + view is de-allocated. While the shadow buffer is + being used, it will have an exclusive write lock on + the original buffer. */ PyObject * @@ -528,7 +528,7 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key) /* Return a new memory-view object */ Py_buffer newview; memset(&newview, 0, sizeof(newview)); - /* XXX: This needs to be fixed so it + /* XXX: This needs to be fixed so it actually returns a sub-view */ return PyMemoryView_FromMemory(&newview); |