Discussion:
[Python-3000-checkins] r65420 - in python/branches/py3k: Lib/test/test_bytes.py Lib/test/test_socket.py Objects/bytesobject.c Objects/memoryobject.c
antoine.pitrou
2008-08-02 21:02:48 UTC
Permalink
Author: antoine.pitrou
Date: Sat Aug 2 23:02:48 2008
New Revision: 65420

Log:
#2538: bytes objects can only provide read-only buffers



Modified:
python/branches/py3k/Lib/test/test_bytes.py
python/branches/py3k/Lib/test/test_socket.py
python/branches/py3k/Objects/bytesobject.c
python/branches/py3k/Objects/memoryobject.c

Modified: python/branches/py3k/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k/Lib/test/test_bytes.py (original)
+++ python/branches/py3k/Lib/test/test_bytes.py Sat Aug 2 23:02:48 2008
@@ -453,6 +453,11 @@
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


Modified: python/branches/py3k/Lib/test/test_socket.py
==============================================================================
--- python/branches/py3k/Lib/test/test_socket.py (original)
+++ python/branches/py3k/Lib/test/test_socket.py Sat Aug 2 23:02:48 2008
@@ -1112,7 +1112,7 @@
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 @@
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)]

Modified: python/branches/py3k/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k/Objects/bytesobject.c (original)
+++ python/branches/py3k/Objects/bytesobject.c Sat Aug 2 23:02:48 2008
@@ -965,7 +965,7 @@
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 = {

Modified: python/branches/py3k/Objects/memoryobject.c
==============================================================================
--- python/branches/py3k/Objects/memoryobject.c (original)
+++ python/branches/py3k/Objects/memoryobject.c Sat Aug 2 23:02:48 2008
@@ -56,7 +56,7 @@
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 @@
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 @@
/* 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);

Loading...