[New-bugs-announce] [issue26720] memoryview from BufferedWriter becomes garbage

Martin Panter report at bugs.python.org
Sat Apr 9 08:08:46 EDT 2016

New submission from Martin Panter:

>>> class Raw(RawIOBase):
...     def writable(self): return True
...     def write(self, b):
...         global written
...         written = b
...         return len(b)
>>> writer = BufferedWriter(Raw())
>>> writer.write(b"blaua")
>>> raw = writer.detach()
>>> written
<memory at 0x7fd37f7b1aa8>
>>> written.tobytes()
>>> del writer
>>> written.tobytes()  # Garbage

Assuming this is pointing into unallocated memory, maybe it could trigger a segfault, though I haven’t seen that.

I haven’t looked at the implementation. But I am guessing that BufferedWriter is passing a view of its internal buffer to write(). For Python 2, perhaps the fix is to check if that memoryview is still referenced, and allocate a new buffer if so. 3.5 should probably inherit this fix.

Another option for 3.6 might be to call release() when write() returns. This should be documented (along with the fact that memoryview is possible in the first place; see Issue 20699).

components: IO
messages: 263083
nosy: martin.panter
priority: normal
severity: normal
status: open
title: memoryview from BufferedWriter becomes garbage
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list