Skip to content
Merged
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
8 changes: 4 additions & 4 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def nreadahead():
res += b
if res.endswith(b"\n"):
break
return bytes(res)
return res.take_bytes()

def __iter__(self):
self._checkClosed()
Expand Down Expand Up @@ -620,15 +620,15 @@ def read(self, size=-1):
if n < 0 or n > len(b):
raise ValueError(f"readinto returned {n} outside buffer size {len(b)}")
del b[n:]
Copy link
Member

Choose a reason for hiding this comment

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

Is there a difference between b.resize(n) and del b[n:]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.resize() could grow while the del b[n:] never will. There's some code which does a = bytearray(b'12'); del a[3:] (intentionally del a slice past the end) as part of its buffer management. Generally I prefer resize because there's a lot less code involved than the slice code (and the slice code eventually calls resize anyways)

return bytes(b)
return b.take_bytes()

def readall(self):
"""Read until EOF, using multiple read() call."""
res = bytearray()
while data := self.read(DEFAULT_BUFFER_SIZE):
res += data
if res:
return bytes(res)
return res.take_bytes()
else:
# b'' or None
return data
Expand Down Expand Up @@ -1738,7 +1738,7 @@ def readall(self):
assert len(result) - bytes_read >= 1, \
"os.readinto buffer size 0 will result in erroneous EOF / returns 0"
result.resize(bytes_read)
return bytes(result)
return result.take_bytes()

def readinto(self, buffer):
"""Same as RawIOBase.readinto()."""
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,8 @@ def test_flush_and_readinto(self):
def _readinto(bufio, n=-1):
b = bytearray(n if n >= 0 else 9999)
n = bufio.readinto(b)
return bytes(b[:n])
b.resize(n)
return b.take_bytes()
self.check_flush_and_read(_readinto)

def test_flush_and_peek(self):
Expand Down
6 changes: 2 additions & 4 deletions Lib/test/test_io/test_largefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class TestFileMethods(LargeFileTest):
(i.e. > 2 GiB) files.
"""

# _pyio.FileIO.readall() uses a temporary bytearray then casted to bytes,
# so memuse=2 is needed
@bigmemtest(size=size, memuse=2, dry_run=False)
@bigmemtest(size=size, memuse=1, dry_run=False)
def test_large_read(self, _size):
# bpo-24658: Test that a read greater than 2GB does not fail.
with self.open(TESTFN, "rb") as f:
Expand Down Expand Up @@ -154,7 +152,7 @@ def test_seekable(self):
f.seek(pos)
self.assertTrue(f.seekable())

@bigmemtest(size=size, memuse=2, dry_run=False)
@bigmemtest(size=size, memuse=1, dry_run=False)
def test_seek_readall(self, _size):
# Seek which doesn't change position should readall successfully.
with self.open(TESTFN, 'rb') as f:
Expand Down
Loading