diff -r a16403affccd Lib/test/test_io.py --- a/Lib/test/test_io.py Wed Aug 01 03:57:52 2012 +0200 +++ b/Lib/test/test_io.py Wed Aug 01 05:16:30 2012 +0200 @@ -2720,6 +2720,12 @@ support.gc_collect() self.assertTrue(wr() is None, wr) + def test_buffer_freeing(self) : + f = open(__file__, "rb") + f_size = sys.getsizeof(f) + f.close() + self.assertGreater(f_size, sys.getsizeof(f)) + def test_abcs(self): # Test the visible base classes are ABCs. self.assertIsInstance(self.IOBase, abc.ABCMeta) diff -r a16403affccd Misc/NEWS --- a/Misc/NEWS Wed Aug 01 03:57:52 2012 +0200 +++ b/Misc/NEWS Wed Aug 01 05:16:30 2012 +0200 @@ -53,6 +53,9 @@ - Issue #15291: Fix a memory leak where AST nodes where not properly deallocated. +- Issue #15448: Buffered IO now frees the buffer when closed, instead + of when deallocating. + - Issue #15110: Fix the tracebacks generated by "import xxx" to not show the importlib stack frames. diff -r a16403affccd Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Wed Aug 01 03:57:52 2012 +0200 +++ b/Modules/_io/bufferedio.c Wed Aug 01 05:16:30 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;