Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -727,6 +727,8 @@ def readinto1(self, b):
def _readinto(self, b, read1):
if not isinstance(b, memoryview):
b = memoryview(b)
if b.readonly:
raise TypeError("readinto() argument must be read-write bytes-like object")
Comment thread
picnixz marked this conversation as resolved.
b = b.cast('B')

if read1:
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,23 @@ def test_readinto(self):
self.assertEqual(bufio.readinto(b), 1)
self.assertEqual(b, b"cb")

@support.subTests("method", ("readinto", "readinto1"))
@support.subTests("buffer", (
b"xxx", memoryview(b"xxx"),
))
@support.subTests("position", (0, 4))
@support.subTests("buffered", (False, True))
def test_readinto_readonly_buffer(self, method, buffer, position, buffered):
data = b"abcdef"
with self.tp(self.BytesIO(data)) as bufio:
bufio.seek(position)
if buffered:
bufio.peek(1)
op = getattr(bufio, method)
self.assertRaises(TypeError, op, buffer)
self.assertEqual(bufio.tell(), position)
self.assertEqual(bufio.read(), data[position:])

def test_readinto1(self):
buffer_size = 10
rawio = self.MockRawIO((b"abc", b"de", b"fgh", b"jkl"))
Expand Down
14 changes: 14 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,20 @@ def test_readinto(self):
memio.readinto(b)
self.assertEqual(b, b"")

@support.subTests("method", ("readinto", "readinto1"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we need the same test with memory IO (StringIO and BytesIO) and BufferedIO would be better to write it once as a test helper or put somewhere both can use rather than the duplicated implementation (there's a test_io.utils which may work well)

@support.subTests("buffer", (
b"xxx", memoryview(b"xxx"), b"", memoryview(b""),
))
@support.subTests("position", (0, 4, 10))
def test_readinto_readonly_buffer(self, method, buffer, position):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this would fit better in test_bufferedio. Goal for me is to test all the Buffered I/O implementations readinto a readonly buffer doesn't advance. That the others implement it right already is nice, but a good thing to make sure we keep.

test_general IOTest would also work well as a spot but I don't like it quite as much just because it keeps adding to the big ball of general tests.

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.

Thanks! Added a shared test in BufferedReaderTest in f46989f, covering both BufferedReader and BufferedRandom in the C and Python implementations. It checks readinto and readinto1, readonly bytes and memoryviews, and both prefilled and unfilled read buffers. The test verifies that the failed call preserves the logical position and subsequent readable data. I retained the BytesIO regression test to cover the original bug directly.

The shared test uses nonempty targets with data remaining: the Python buffered readers currently return 0 for empty targets or at EOF, whereas the C implementations reject them. The existing BytesIO regression still covers those edge cases.

The full test_io suite passes (1,047 tests, 35 skipped).

data = b"abcdef"
with self.ioclass(data) as memio:
memio.seek(position)
op = getattr(memio, method)
self.assertRaises(TypeError, op, 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