Python 3.16.0a0 (heads/main:09117bc, Sep 6 2026, 15:04:05) [Clang 21.0.0 (clang-2100.1.1.101)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
...
... stream = io.BytesIO(b"abcd")
... stream.seek(2)
... view = stream.getbuffer()
... try:
... stream.__init__(b"replacement")
... except BufferError:
... pass
... print(stream.tell(), stream.getvalue(), bytes(view))
... # 0 b'' b'abcd'
... view.release()
...
0 b'' b'abcd'
Bug report
Bug description:
This is happening because the implementation of
BytesIO.__init__resets state before checking for theBufferError:cpython/Modules/_io/bytesio.c
Lines 1129 to 1137 in 09117bc
CPython versions tested on:
3.16, CPython main branch
Operating systems tested on:
macOS
Linked PRs