Index: Lib/test/test_memoryio.py =================================================================== --- Lib/test/test_memoryio.py (revision 73313) +++ Lib/test/test_memoryio.py (working copy) @@ -9,6 +9,7 @@ import io import _pyio as pyio import sys + class MemorySeekTestMixin: @@ -338,7 +339,14 @@ self.assertEqual(test1(), buf) self.assertEqual(test2(), buf) + def test_instance_dict_leak(self): + # Test case for issue #6242. + # This will be caught by regrtest.py -R if this leak. + for _ in range(100): + memio = self.ioclass() + memio.foo = 1 + class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase): UnsupportedOperation = pyio.UnsupportedOperation Index: Modules/_io/bytesio.c =================================================================== --- Modules/_io/bytesio.c (revision 73313) +++ Modules/_io/bytesio.c (working copy) @@ -606,14 +606,31 @@ Py_RETURN_NONE; } +static int +bytesio_traverse(BytesIOObject *self, visitproc visit, void *arg) +{ + Py_VISIT(self->dict); + return 0; +} + +static int +bytesio_clear(BytesIOObject *self) +{ + Py_CLEAR(self->dict); + return 0; +} + static void bytesio_dealloc(BytesIOObject *self) { - if (self->buf != NULL) { + _PyObject_GC_UNTRACK(self); + if (self->buf) { PyMem_Free(self->buf); self->buf = NULL; } - Py_TYPE(self)->tp_clear((PyObject *)self); + Py_CLEAR(self->dict); + if (self->weakreflist != NULL) + PyObject_ClearWeakRefs((PyObject *)self); Py_TYPE(self)->tp_free(self); } @@ -663,24 +680,6 @@ return 0; } -static int -bytesio_traverse(BytesIOObject *self, visitproc visit, void *arg) -{ - Py_VISIT(self->dict); - Py_VISIT(self->weakreflist); - return 0; -} - -static int -bytesio_clear(BytesIOObject *self) -{ - Py_CLEAR(self->dict); - if (self->weakreflist != NULL) - PyObject_ClearWeakRefs((PyObject *)self); - return 0; -} - - static PyGetSetDef bytesio_getsetlist[] = { {"closed", (getter)bytesio_get_closed, NULL, "True if the file is closed."}, Index: Modules/_io/stringio.c =================================================================== --- Modules/_io/stringio.c (revision 73313) +++ Modules/_io/stringio.c (working copy) @@ -509,11 +509,15 @@ stringio_dealloc(StringIOObject *self) { _PyObject_GC_UNTRACK(self); + self->ok = 0; + if (self->buf) { + PyMem_Free(self->buf); + self->buf = NULL; + } Py_CLEAR(self->readnl); Py_CLEAR(self->writenl); Py_CLEAR(self->decoder); - if (self->buf) - PyMem_Free(self->buf); + Py_CLEAR(self->dict); if (self->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); Py_TYPE(self)->tp_free(self);