diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 76a27910da4d5f..0be61ff8a959f8 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -10,6 +10,7 @@ import sys # Import _thread instead of threading to reduce startup cost from _thread import allocate_lock as Lock +from itertools import repeat if sys.platform in {'win32', 'cygwin'}: from msvcrt import setmode as _setmode else: @@ -1686,7 +1687,8 @@ def readall(self): if addend < DEFAULT_BUFFER_SIZE: addend = DEFAULT_BUFFER_SIZE bufsize += addend - result[bytes_read:bufsize] = b'\0' + result.extend(repeat(0, addend)) + assert len(result) == bufsize, "Should have expanded in size" assert bufsize - bytes_read > 0, "Should always try and read at least one byte" try: n = os.readinto(self._fd, memoryview(result)[bytes_read:]) diff --git a/Misc/NEWS.d/next/Library/2025-01-31-19-07-31.gh-issue-129005.II6go0.rst b/Misc/NEWS.d/next/Library/2025-01-31-19-07-31.gh-issue-129005.II6go0.rst new file mode 100644 index 00000000000000..260a8ba63a45d0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-31-19-07-31.gh-issue-129005.II6go0.rst @@ -0,0 +1 @@ +:mod:`!pyio`: Fix expansion of buffer in _pyio.FileIO.readall