changeset: 94480:e62d54128bd3 branch: 3.4 parent: 94477:98c720c3e061 user: Serhiy Storchaka date: Tue Feb 03 02:00:18 2015 +0200 files: Doc/library/io.rst Lib/_pyio.py Lib/test/test_memoryio.py Misc/NEWS Modules/_io/bytesio.c description: Issue #23099: Closing io.BytesIO with exported buffer is rejected now to prevent corrupting exported buffer. diff -r 98c720c3e061 -r e62d54128bd3 Doc/library/io.rst --- a/Doc/library/io.rst Tue Feb 03 01:35:10 2015 +0200 +++ b/Doc/library/io.rst Tue Feb 03 02:00:18 2015 +0200 @@ -563,7 +563,8 @@ .. class:: BytesIO([initial_bytes]) A stream implementation using an in-memory bytes buffer. It inherits - :class:`BufferedIOBase`. + :class:`BufferedIOBase`. The buffer is discarded when the + :meth:`~IOBase.close` method is called. The argument *initial_bytes* contains optional initial :class:`bytes` data. @@ -584,7 +585,7 @@ .. note:: As long as the view exists, the :class:`BytesIO` object cannot be - resized. + resized or closed. .. versionadded:: 3.2 @@ -592,6 +593,7 @@ Return :class:`bytes` containing the entire contents of the buffer. + .. method:: read1() In :class:`BytesIO`, this is the same as :meth:`read`. @@ -858,7 +860,8 @@ .. class:: StringIO(initial_value='', newline='\\n') - An in-memory stream for text I/O. + An in-memory stream for text I/O. The text buffer is discarded when the + :meth:`~IOBase.close` method is called. The initial value of the buffer (an empty string by default) can be set by providing *initial_value*. The *newline* argument works like that of @@ -870,9 +873,7 @@ .. method:: getvalue() - Return a ``str`` containing the entire contents of the buffer at any - time before the :class:`StringIO` object's :meth:`close` method is - called. + Return a ``str`` containing the entire contents of the buffer. Example usage:: diff -r 98c720c3e061 -r e62d54128bd3 Lib/_pyio.py --- a/Lib/_pyio.py Tue Feb 03 01:35:10 2015 +0200 +++ b/Lib/_pyio.py Tue Feb 03 02:00:18 2015 +0200 @@ -833,8 +833,14 @@ def getbuffer(self): """Return a readable and writable view of the buffer. """ + if self.closed: + raise ValueError("getbuffer on closed file") return memoryview(self._buffer) + def close(self): + self._buffer.clear() + super().close() + def read(self, size=None): if self.closed: raise ValueError("read from closed file") diff -r 98c720c3e061 -r e62d54128bd3 Lib/test/test_memoryio.py --- a/Lib/test/test_memoryio.py Tue Feb 03 01:35:10 2015 +0200 +++ b/Lib/test/test_memoryio.py Tue Feb 03 02:00:18 2015 +0200 @@ -398,14 +398,19 @@ # raises a BufferError. self.assertRaises(BufferError, memio.write, b'x' * 100) self.assertRaises(BufferError, memio.truncate) + self.assertRaises(BufferError, memio.close) + self.assertFalse(memio.closed) # Mutating the buffer updates the BytesIO buf[3:6] = b"abc" self.assertEqual(bytes(buf), b"123abc7890") self.assertEqual(memio.getvalue(), b"123abc7890") - # After the buffer gets released, we can resize the BytesIO again + # After the buffer gets released, we can resize and close the BytesIO + # again del buf support.gc_collect() memio.truncate() + memio.close() + self.assertRaises(ValueError, memio.getbuffer) class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, diff -r 98c720c3e061 -r e62d54128bd3 Misc/NEWS --- a/Misc/NEWS Tue Feb 03 01:35:10 2015 +0200 +++ b/Misc/NEWS Tue Feb 03 02:00:18 2015 +0200 @@ -56,6 +56,9 @@ Library ------- +- Issue #23099: Closing io.BytesIO with exported buffer is rejected now to + prevent corrupting exported buffer. + - Issue #23363: Fix possible overflow in itertools.permutations. - Issue #23364: Fix possible overflow in itertools.product. diff -r 98c720c3e061 -r e62d54128bd3 Modules/_io/bytesio.c --- a/Modules/_io/bytesio.c Tue Feb 03 01:35:10 2015 +0200 +++ b/Modules/_io/bytesio.c Tue Feb 03 02:00:18 2015 +0200 @@ -657,6 +657,7 @@ static PyObject * bytesio_close(bytesio *self) { + CHECK_EXPORTS(self); if (self->buf != NULL) { PyMem_Free(self->buf); self->buf = NULL;