diff -r da3badae1765 -r 53aa92a70127 Lib/test/test_io.py --- a/Lib/test/test_io.py Thu Oct 04 02:59:09 2012 +0200 +++ b/Lib/test/test_io.py Thu Oct 04 12:30:32 2012 +0200 @@ -815,6 +815,14 @@ 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 -r da3badae1765 -r 53aa92a70127 Misc/NEWS --- a/Misc/NEWS Thu Oct 04 02:59:09 2012 +0200 +++ b/Misc/NEWS Thu Oct 04 12:30:32 2012 +0200 @@ -24,6 +24,9 @@ - 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 -r da3badae1765 -r 53aa92a70127 Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Thu Oct 04 02:59:09 2012 +0200 +++ b/Modules/_io/bufferedio.c Thu Oct 04 12:30:32 2012 +0200 @@ -519,6 +519,11 @@ 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;