Jeffrey Kintscher <websur...@surf2c.net> added the comment:

>>> 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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41097>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to