mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-60107: Remove a copy from RawIOBase.read (#141532)
If the underlying I/O class keeps a reference to the memory, raise BufferError. Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
722f4bb8c9
commit
e90061f5f1
7 changed files with 39 additions and 11 deletions
|
|
@ -609,6 +609,25 @@ def readinto(self, b):
|
|||
with self.assertRaises(ValueError):
|
||||
Misbehaved(bad_size).read()
|
||||
|
||||
def test_RawIOBase_read_gh60107(self):
|
||||
# gh-60107: Ensure a "Raw I/O" which keeps a reference to the
|
||||
# mutable memory doesn't allow making a mutable bytes.
|
||||
class RawIOKeepsReference(self.MockRawIOWithoutRead):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.buf = None
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def readinto(self, buf):
|
||||
# buf is the bytearray so keeping a reference to it doesn't keep
|
||||
# the memory alive; a memoryview does.
|
||||
self.buf = memoryview(buf)
|
||||
buf[0:4] = self._read_stack.pop()
|
||||
return 3
|
||||
|
||||
with self.assertRaises(BufferError):
|
||||
rawio = RawIOKeepsReference([b"1234"])
|
||||
rawio.read(4)
|
||||
|
||||
def test_types_have_dict(self):
|
||||
test = (
|
||||
self.IOBase(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue