From a88183713b4041d4cddd1f8321021e54f4c966fe Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Tue, 28 Jan 2025 21:57:09 -0800 Subject: [PATCH] gh-129005: Remove copy in `_pyio.FileIO.readall()` This aligns the memory usage between _pyio and _io. Both now use the same amount of memory when reading a file. --- Lib/_pyio.py | 2 +- Lib/test/test_largefile.py | 6 ++---- .../Library/2025-01-28-21-51-50.gh-issue-129005.OIdODu.rst | 2 ++ 3 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-01-28-21-51-50.gh-issue-129005.OIdODu.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 755e0258770891..7d96044f2ce23f 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1702,7 +1702,7 @@ def readall(self): bytes_read += n del result[bytes_read:] - return bytes(result) + return result def readinto(self, buffer): """Same as RawIOBase.readinto().""" diff --git a/Lib/test/test_largefile.py b/Lib/test/test_largefile.py index 41f7b70e5cfe81..438a90a92ed588 100644 --- a/Lib/test/test_largefile.py +++ b/Lib/test/test_largefile.py @@ -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: @@ -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: diff --git a/Misc/NEWS.d/next/Library/2025-01-28-21-51-50.gh-issue-129005.OIdODu.rst b/Misc/NEWS.d/next/Library/2025-01-28-21-51-50.gh-issue-129005.OIdODu.rst new file mode 100644 index 00000000000000..595b39d56dbb36 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-28-21-51-50.gh-issue-129005.OIdODu.rst @@ -0,0 +1,2 @@ +:mod:`!_pyio`: Return bytearray from ``_pyio.FileIO.readall()`` rather than +copying into a bytes. Memory usage now matches ``_io.FileIO.readall()``