diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 023478aa78c6a04..5409a368469f7e0 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1674,22 +1674,29 @@ def readall(self): except OSError: pass - result = bytearray() + result = bytearray(bufsize) + bytes_read = 0 while True: if len(result) >= bufsize: - bufsize = len(result) - bufsize += max(bufsize, DEFAULT_BUFFER_SIZE) - n = bufsize - len(result) + # Parallels _io/fileio.c new_buffersize + if bufsize > 65536: + addend = bufsize >> 3 + else: + addend = 256 + bufsize + if addend < DEFAULT_BUFFER_SIZE: + addend = DEFAULT_BUFFER_SIZE + bufsize += addend + result.resize(bufsize) try: - chunk = os.read(self._fd, n) + n = os.readinto(self._fd, memoryview(result)[bytes_read:]) except BlockingIOError: - if result: + if bytes_read: break return None - if not chunk: # reached the end of the file + if n == 0: # Reached the end of the file break - result += chunk - + bytes_read += n + result.resize(bytes_read) return bytes(result) def readinto(self, buffer):