Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f89aa6

Browse files
committed
#2538: bytes objects can only provide read-only buffers
1 parent 07431a3 commit 2f89aa6

4 files changed

Lines changed: 13 additions & 8 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ def test_ord(self):
453453
class BytesTest(BaseBytesTest):
454454
type2test = bytes
455455

456+
def test_buffer_is_readonly(self):
457+
with open(sys.stdin.fileno(), "rb", buffering=0) as f:
458+
self.assertRaises(TypeError, f.readinto, b"")
459+
460+
456461
class ByteArrayTest(BaseBytesTest):
457462
type2test = bytearray
458463

Lib/test/test_socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ def __init__(self, methodName='runTest'):
11121112
SocketConnectedTest.__init__(self, methodName=methodName)
11131113

11141114
def testRecvInto(self):
1115-
buf = b" "*1024
1115+
buf = bytearray(1024)
11161116
nbytes = self.cli_conn.recv_into(buf)
11171117
self.assertEqual(nbytes, len(MSG))
11181118
msg = buf[:len(MSG)]
@@ -1123,7 +1123,7 @@ def _testRecvInto(self):
11231123
self.serv_conn.send(buf)
11241124

11251125
def testRecvFromInto(self):
1126-
buf = b" "*1024
1126+
buf = bytearray(1024)
11271127
nbytes, addr = self.cli_conn.recvfrom_into(buf)
11281128
self.assertEqual(nbytes, len(MSG))
11291129
msg = buf[:len(MSG)]

Objects/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ static int
965965
string_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags)
966966
{
967967
return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_SIZE(self),
968-
0, flags);
968+
1, flags);
969969
}
970970

971971
static PySequenceMethods string_as_sequence = {

Objects/memoryobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ PyMemoryView_FromObject(PyObject *base)
5656
if (mview == NULL) return NULL;
5757

5858
mview->base = NULL;
59-
if (PyObject_GetBuffer(base, &(mview->view), PyBUF_FULL) < 0) {
59+
if (PyObject_GetBuffer(base, &(mview->view), PyBUF_FULL_RO) < 0) {
6060
Py_DECREF(mview);
6161
return NULL;
6262
}
@@ -204,9 +204,9 @@ _indirect_copy_nd(char *dest, Py_buffer *view, char fort)
204204
a contiguous buffer if it is not. The view will point to
205205
the shadow buffer which can be written to and then
206206
will be copied back into the other buffer when the memory
207-
view is de-allocated. While the shadow buffer is
208-
being used, it will have an exclusive write lock on
209-
the original buffer.
207+
view is de-allocated. While the shadow buffer is
208+
being used, it will have an exclusive write lock on
209+
the original buffer.
210210
*/
211211

212212
PyObject *
@@ -528,7 +528,7 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
528528
/* Return a new memory-view object */
529529
Py_buffer newview;
530530
memset(&newview, 0, sizeof(newview));
531-
/* XXX: This needs to be fixed so it
531+
/* XXX: This needs to be fixed so it
532532
actually returns a sub-view
533533
*/
534534
return PyMemoryView_FromMemory(&newview);

0 commit comments

Comments
 (0)