[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data
Arthur Darcet
report at bugs.python.org
Tue Jul 25 08:03:58 EDT 2017
New submission from Arthur Darcet:
If I'm not mistaken, a BytesIO buffer can be in three states:
(1) `b = BytesIO(b'data')` -> free of any constraints
(2) `d = b'data'; b = BytesIO(d)` -> cannot modify the underlying bytes without copying them
(3) `b = BytesIO(b'data'); d = b.getbuffer()` -> cannot return a "bytes" representation of the data without copying it (the underlying buffer might change)
My use-case is "how to get the length of the data currently in the BytesIO object".
And right now, there are two solutions:
(a) `len(b.getvalue())`
(b) `len(b.getbuffer())`
but, solution (a) is copying data if the buffer is in state (3) ; and solution (b) is copying data for state (2).
And I don't see any way to distinguish between the three states from Python code.
So as far as I understand it, there is no way to get the size of the buffer in Python that would reliably not copy any data
Should I open a PR to add a `size()` method on the BytesIO class? (simply returning `PyLong_FromSsize_t(self->string_size)`
----------
components: IO
messages: 299054
nosy: rthr
priority: normal
severity: normal
status: open
title: io.BytesIO: no way to get the length of the underlying buffer without copying data
type: enhancement
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31025>
_______________________________________
More information about the Python-bugs-list
mailing list