gh-129005: Update _pyio.BytesIO to use bytearray.resize on write (#129702)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Cody Maloney 2025-02-06 02:18:08 -08:00 committed by GitHub
parent d83a8a26f5
commit 052ca8ffe8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -937,10 +937,8 @@ class BytesIO(BufferedIOBase):
return 0
pos = self._pos
if pos > len(self._buffer):
# Inserts null bytes between the current end of the file
# and the new write position.
padding = b'\x00' * (pos - len(self._buffer))
self._buffer += padding
# Pad buffer to pos with null bytes.
self._buffer.resize(pos)
self._buffer[pos:pos + n] = b
self._pos += n
return n