]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129005: Update _pyio.BytesIO to use bytearray.resize on write (#129702)
authorCody Maloney <cmaloney@users.noreply.github.com>
Thu, 6 Feb 2025 10:18:08 +0000 (02:18 -0800)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2025 10:18:08 +0000 (10:18 +0000)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/_pyio.py

index 023478aa78c6a04b2320f3a360fe1be9e020bf6d..b3a8f37d68acdb77c9b0c2f7487eb82bddbce14a 100644 (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