The documentation of IOBase.truncate
method says that:
truncate(size=None)
Resize the stream to the given size in bytes (or the current position if size is not specified). The current stream position isn’t changed. This resizing can extend or reduce the current file size. In case of extension, the contents of the new file area depend on the platform (on most systems, additional bytes are zero-filled). The new file size is returned.
Changed in version 3.5: Windows will now zero-fill files when extending.
So, taking this into account I suppose that BytesIO
(that is a subclass of BufferedIOBase
which in turn is a subclass of IOBase
) changes its internal buffer size after this method has been called.
But the following code snippet shows that I'm wrong in my assumptions:
from io import BytesIO
# prints b'\x00\x00\x00\x00\x00\x00\x00\x00'
data = BytesIO(8 * b"\x00")
print(data.getvalue())
# prints 16
print(data.truncate(16))
# prints b'\x00\x00\x00\x00\x00\x00\x00\x00'
print(data.getvalue())
# prints b'\x00\x00\x00\x00\x00\x00\x00\x00'
print(bytes(data.getbuffer()))
Where did I turn the wrong way?