Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ def _readinto(self, b, read1):
if not isinstance(b, memoryview):
b = memoryview(b)
b = b.cast('B')
if b.readonly:
raise TypeError("readinto() argument must be read-write bytes-like object")
Comment thread
picnixz marked this conversation as resolved.

if read1:
data = self.read1(len(b))
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,21 @@ def test_readinto(self):
memio.readinto(b)
self.assertEqual(b, b"")

def test_readinto_readonly_buffer(self):
data = b"abcdef"
for method in ("readinto", "readinto1"):
for buffer in (b"xxx", memoryview(b"xxx"),
b"", memoryview(b"")):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use support.subTests() for parametrizing the method and buffer. It would be easier.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the test to use support.subTests() in 3831e0c, keeping all parameter combinations. The full test_io suite passes.

for position in (0, 4, 10):
with self.subTest(method=method, buffer=buffer,
position=position):
with self.ioclass(data) as memio:
memio.seek(position)
with self.assertRaises(TypeError):
getattr(memio, method)(buffer)
self.assertEqual(memio.tell(), position)
self.assertEqual(memio.read(), data[position:])

def test_relative_seek(self):
buf = self.buftype("1234567890")
memio = self.ioclass(buf)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The pure Python implementation of :meth:`io.BufferedIOBase.readinto` and
:meth:`~io.BufferedIOBase.readinto1` now rejects read-only buffers before
reading input, without changing the stream position.
Loading