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")
5
>>> raw = writer.detach()
>>> written
<memory at 0x7fd37f7b1aa8>
>>> written.tobytes()
b'blaua'
>>> del writer
>>> written.tobytes()  # Garbage
b'\x80f\xab\x00\x00'

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 <rep...@bugs.python.org>
<http://bugs.python.org/issue26720>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to