diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-03 07:30:51 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-03 07:30:51 (GMT) |
commit | 32ca3dcb97a75c05dc2b90c88bbf82a541c57c61 (patch) | |
tree | 60e9b695fbc4017a34f2202beef337eb01c347db /Lib | |
parent | 7a27c97216ef1a71bc26a559d7331523875a0c6f (diff) | |
parent | c057c3859c68f2e86c7a492ae8f8b4b3a3b136c8 (diff) | |
download | cpython-32ca3dcb97a75c05dc2b90c88bbf82a541c57c61.zip cpython-32ca3dcb97a75c05dc2b90c88bbf82a541c57c61.tar.gz cpython-32ca3dcb97a75c05dc2b90c88bbf82a541c57c61.tar.bz2 |
Issue #23099: Closing io.BytesIO with exported buffer is rejected now to
prevent corrupting exported buffer.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_pyio.py | 6 | ||||
-rw-r--r-- | Lib/test/test_memoryio.py | 7 |
2 files changed, 12 insertions, 1 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 3614644..98b87f7 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -852,8 +852,14 @@ class BytesIO(BufferedIOBase): 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 --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py index ab67cf3..24d282a 100644 --- a/Lib/test/test_memoryio.py +++ b/Lib/test/test_memoryio.py @@ -399,14 +399,19 @@ class BytesIOMixin: # 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, |