From dc469454ec997166aea062f5727a8d59bab2f7a0 Mon Sep 17 00:00:00 2001 From: Jesus Cea Date: Thu, 4 Oct 2012 12:37:56 +0200 Subject: Closes #15488: Closed files keep their buffer alive --- Lib/test/test_io.py | 8 ++++++++ Misc/NEWS | 3 +++ Modules/_io/bufferedio.c | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index d5eec7c..ec0501e 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -815,6 +815,14 @@ class SizeofTest: bufio = self.tp(rawio, buffer_size=bufsize2) self.assertEqual(sys.getsizeof(bufio), size + bufsize2) + @support.cpython_only + def test_buffer_freeing(self) : + bufsize = 4096 + rawio = self.MockRawIO() + bufio = self.tp(rawio, buffer_size=bufsize) + size = sys.getsizeof(bufio) - bufsize + bufio.close() + self.assertEqual(sys.getsizeof(bufio), size) class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): read_mode = "rb" diff --git a/Misc/NEWS b/Misc/NEWS index 3b8ec21..042b3cc 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -24,6 +24,9 @@ Core and Builtins - Issue #15839: Convert SystemErrors in `super()` to RuntimeErrors. +- Issue #15448: Buffered IO now frees the buffer when closed, instead + of when deallocating. + - Issue #15846: Fix SystemError which happened when using `ast.parse()` in an exception handler on code with syntax errors. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 334734b..432349a 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -519,6 +519,11 @@ buffered_close(buffered *self, PyObject *args) res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL); + if (self->buffer) { + PyMem_Free(self->buffer); + self->buffer = NULL; + } + end: LEAVE_BUFFERED(self) return res; -- cgit v0.12