Skip to content

Commit 853a6b7

Browse files
authored
Revert "gh-129005: Align FileIO.readall() allocation (#129458)" (#129572)
This reverts commit f927204.
1 parent 237f186 commit 853a6b7

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

Lib/_pyio.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,31 +1674,22 @@ def readall(self):
16741674
except OSError:
16751675
pass
16761676

1677-
result = bytearray(bufsize)
1678-
bytes_read = 0
1677+
result = bytearray()
16791678
while True:
1680-
if bytes_read >= bufsize:
1681-
# Parallels _io/fileio.c new_buffersize
1682-
if bufsize > 65536:
1683-
addend = bufsize >> 3
1684-
else:
1685-
addend = bufsize + 256
1686-
if addend < DEFAULT_BUFFER_SIZE:
1687-
addend = DEFAULT_BUFFER_SIZE
1688-
bufsize += addend
1689-
result[bytes_read:bufsize] = b'\0'
1690-
assert bufsize - bytes_read > 0, "Should always try and read at least one byte"
1679+
if len(result) >= bufsize:
1680+
bufsize = len(result)
1681+
bufsize += max(bufsize, DEFAULT_BUFFER_SIZE)
1682+
n = bufsize - len(result)
16911683
try:
1692-
n = os.readinto(self._fd, memoryview(result)[bytes_read:])
1684+
chunk = os.read(self._fd, n)
16931685
except BlockingIOError:
1694-
if bytes_read > 0:
1686+
if result:
16951687
break
16961688
return None
1697-
if n == 0: # reached the end of the file
1689+
if not chunk: # reached the end of the file
16981690
break
1699-
bytes_read += n
1691+
result += chunk
17001692

1701-
del result[bytes_read:]
17021693
return bytes(result)
17031694

17041695
def readinto(self, buffer):

Misc/NEWS.d/next/Library/2025-01-28-21-22-44.gh-issue-129005.h57i9j.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)