From: Cody Maloney Date: Thu, 6 Feb 2025 10:18:08 +0000 (-0800) Subject: gh-129005: Update _pyio.BytesIO to use bytearray.resize on write (#129702) X-Git-Tag: v3.14.0a5~96 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=052ca8ffe8c57afb9c270fcc4eb5f390cbcfb8ce;p=thirdparty%2FPython%2Fcpython.git gh-129005: Update _pyio.BytesIO to use bytearray.resize on write (#129702) Co-authored-by: Victor Stinner --- diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 023478aa78c6..b3a8f37d68ac 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -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