From: Cody Maloney Date: Sun, 2 Feb 2025 14:59:15 +0000 (-0800) Subject: Revert "gh-129005: Align FileIO.readall() allocation (#129458)" (#129572) X-Git-Tag: v3.14.0a5~135 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=853a6b7de222964d8cd0e9478cb5c78d490032e6;p=thirdparty%2FPython%2Fcpython.git Revert "gh-129005: Align FileIO.readall() allocation (#129458)" (#129572) This reverts commit f927204f64b3f8dbecec784e05bc8e25d2a78b2e. --- diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 76a27910da4d..023478aa78c6 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1674,31 +1674,22 @@ class FileIO(RawIOBase): except OSError: pass - result = bytearray(bufsize) - bytes_read = 0 + result = bytearray() while True: - if bytes_read >= bufsize: - # Parallels _io/fileio.c new_buffersize - if bufsize > 65536: - addend = bufsize >> 3 - else: - addend = bufsize + 256 - if addend < DEFAULT_BUFFER_SIZE: - addend = DEFAULT_BUFFER_SIZE - bufsize += addend - result[bytes_read:bufsize] = b'\0' - assert bufsize - bytes_read > 0, "Should always try and read at least one byte" + if len(result) >= bufsize: + bufsize = len(result) + bufsize += max(bufsize, DEFAULT_BUFFER_SIZE) + n = bufsize - len(result) try: - n = os.readinto(self._fd, memoryview(result)[bytes_read:]) + chunk = os.read(self._fd, n) except BlockingIOError: - if bytes_read > 0: + if result: break return None - if n == 0: # reached the end of the file + if not chunk: # reached the end of the file break - bytes_read += n + result += chunk - del result[bytes_read:] return bytes(result) def readinto(self, buffer): diff --git a/Misc/NEWS.d/next/Library/2025-01-28-21-22-44.gh-issue-129005.h57i9j.rst b/Misc/NEWS.d/next/Library/2025-01-28-21-22-44.gh-issue-129005.h57i9j.rst deleted file mode 100644 index c76fb05e196f..000000000000 --- a/Misc/NEWS.d/next/Library/2025-01-28-21-22-44.gh-issue-129005.h57i9j.rst +++ /dev/null @@ -1,2 +0,0 @@ -``_pyio.FileIO.readall()`` now allocates, resizes, and fills a data buffer using -the same algorithm ``_io.FileIO.readall()`` uses.