-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
confusing BufferError: Existing exports of data: object cannot be re-sized #85269
Comments
The behavior (tested in 3.6 and 3.9) of io.BytesIO().getbuffer() gives a unexpected exception message: >>> b = io.BytesIO()
>>> b.write(b'abc')
3
>>> buf = b.getbuffer()
>>> b.seek(0)
0
>>> b.write(b'?') # or anything up to 3 bytes
BufferError: Existing exports of data: object cannot be re-sized The error message pretends that the problem is in resizing the BytesIO object, but the write() is not actually causing any resize. I am not sure if the bug is a wrong error message (and all writes are supposed to be forbidden) or a wrongly forbidden write() (after all, we can use the buffer itself to write into the same area of memory). |
I'm getting the same error (in 3.8.3) for this code: o = bytearray(1)
o += o There's no error if the second line is replaced with: o += bytearray(1) Not sure if this is related. |
The error message is found in three different locations in the source code. One is in the bytearray class, and the other two are in the BytesIO class. They are unrelated code paths. |
>>> import io
>>> b = io.BytesIO()
>>> b.write(b'abc')
3
>>> buf = b.getbuffer()
>>> b.seek(0)
0
>>> b.write(b'?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
>>> The problem is caused by the b.getbuffer() call. It increments a reference counter in the BytesIO object that causes the b.write() call to fail because the counter is > 0. The error message is misleading. The counter is decremented when the buffer view is deleted. >>> import io
>>> b = io.BytesIO()
>>> b.write(b'abc')
3
>>> buf = b.getbuffer()
>>> b.seek(0)
0
>>> b.write(b'?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
>>> del buf
>>> b.write(b'?')
1
>>> b.getvalue()
b'?bc'
>>> The documentation for io.BytesIO.getbuffer() says "Note: As long as the view exists, the BytesIO object cannot be resized or closed." Either this is a bug, or the documentation needs to be updated to say the io.BytesIO object is unwritable while any buffer views exist. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: