]> granicus.if.org Git - python/commitdiff
Cleaned up io._BytesIO.write().
authorAlexandre Vassalotti <alexandre@peadrop.com>
Sat, 10 May 2008 19:59:16 +0000 (19:59 +0000)
committerAlexandre Vassalotti <alexandre@peadrop.com>
Sat, 10 May 2008 19:59:16 +0000 (19:59 +0000)
I am amazed that the old code, for inserting null-bytes, actually
worked. Who wrote that thing? Oh, it is me... doh.

Lib/io.py

index 0b6907a0ff46b2527d7c89343bc5462546ea316f..c45062da9979fdcb7e8a10e2729f4bec3dbe9f73 100644 (file)
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -826,14 +826,14 @@ class _BytesIO(BufferedIOBase):
         n = len(b)
         if n == 0:
             return 0
-        newpos = self._pos + n
-        if newpos > len(self._buffer):
+        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' * (newpos - len(self._buffer) - n)
-            self._buffer[self._pos:newpos - n] = padding
-        self._buffer[self._pos:newpos] = b
-        self._pos = newpos
+            padding = b'\x00' * (pos - len(self._buffer))
+            self._buffer += padding
+        self._buffer[pos:pos + n] = b
+        self._pos += n
         return n
 
     def seek(self, pos, whence=0):