[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data

2017-07-26 Thread R. David Murray
R. David Murray added the comment: So you are saying that BytesIO has code that checks that its argument only has a single reference and modifies the string in place when it can if so? You can't depend on that in any other implementation of Python, and shouldn't depend on it in CPython

[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data

2017-07-26 Thread Arthur Darcet
Arthur Darcet added the comment: BytesIO is heavily optimised to avoid copying bytes when it can. For case (1), if you want to modify the data, then there is no need to actually copy it before overwriting it, because no-one else is using it For case (2), if you want to change something, then

[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data

2017-07-25 Thread R. David Murray
R. David Murray added the comment: I'm confused, I don't see how there can be any difference between (1) and (2). -- nosy: +r.david.murray ___ Python tracker

[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data

2017-07-25 Thread Arthur Darcet
Arthur Darcet added the comment: it's a tiny bit slow, but that works, thank you. I guess we can close this % python -m timeit -s "import io; b = io.BytesIO(b'0' * 2 ** 30)" "p = b.tell(); b.seek(0, 2); b.tell(); b.seek(p)" 100 loops, best of 3: 0.615 usec per loop % python -m timeit -s

[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data

2017-07-25 Thread Martin Panter
Martin Panter added the comment: Can’t you use b.seek(0, SEEK_END)? -- nosy: +martin.panter versions: -Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6 ___ Python tracker

[issue31025] io.BytesIO: no way to get the length of the underlying buffer without copying data

2017-07-25 Thread Arthur Darcet
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()`