]> granicus.if.org Git - python/commitdiff
#2538: bytes objects can only provide read-only buffers
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 2 Aug 2008 21:02:48 +0000 (21:02 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 2 Aug 2008 21:02:48 +0000 (21:02 +0000)
Lib/test/test_bytes.py
Lib/test/test_socket.py
Objects/bytesobject.c
Objects/memoryobject.c

index 5bda9354d25c0faaf20dd2ace0cc426ddb51262d..230dbf46bed58aca75738bdb8e2ccae787cfae0b 100644 (file)
@@ -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
 
index 222c42ce7f45a927a53eb1c6b25b5585e6dd55e6..9d753b7ea53c3596f6e1a33ab191fafce61ac6d6 100644 (file)
@@ -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)]
index 471d09c7ec5a747418fb0e7554fee52fdcbadec0..eeae0ff5893d77f54ea43bb6a0e848be03150af0 100644 (file)
@@ -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 = {
index 78ada17aa4bd3a450d24ec7d9bd06a0b02e538ac..79d7db1b0221b5df9fa5b2797967afe384abb6a0 100644 (file)
@@ -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);