FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-158952: Reject read-only buffers before reading in _pyio by emerardd · Pull Request #158953 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions Lib/_pyio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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"))
@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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL