Commit fc477048 authored by Alexandre Vassalotti's avatar Alexandre Vassalotti

Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.

parent 4f1f4227
...@@ -338,6 +338,13 @@ class MemoryTestMixin: ...@@ -338,6 +338,13 @@ class MemoryTestMixin:
self.assertEqual(test1(), buf) self.assertEqual(test1(), buf)
self.assertEqual(test2(), 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): class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
......
...@@ -609,11 +609,14 @@ bytesio_close(bytesio *self) ...@@ -609,11 +609,14 @@ bytesio_close(bytesio *self)
static void static void
bytesio_dealloc(bytesio *self) bytesio_dealloc(bytesio *self)
{ {
_PyObject_GC_UNTRACK(self);
if (self->buf != NULL) { if (self->buf != NULL) {
PyMem_Free(self->buf); PyMem_Free(self->buf);
self->buf = NULL; 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); Py_TYPE(self)->tp_free(self);
} }
...@@ -667,7 +670,6 @@ static int ...@@ -667,7 +670,6 @@ static int
bytesio_traverse(bytesio *self, visitproc visit, void *arg) bytesio_traverse(bytesio *self, visitproc visit, void *arg)
{ {
Py_VISIT(self->dict); Py_VISIT(self->dict);
Py_VISIT(self->weakreflist);
return 0; return 0;
} }
...@@ -675,8 +677,6 @@ static int ...@@ -675,8 +677,6 @@ static int
bytesio_clear(bytesio *self) bytesio_clear(bytesio *self)
{ {
Py_CLEAR(self->dict); Py_CLEAR(self->dict);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
return 0; return 0;
} }
......
...@@ -509,11 +509,15 @@ static void ...@@ -509,11 +509,15 @@ static void
stringio_dealloc(stringio *self) stringio_dealloc(stringio *self)
{ {
_PyObject_GC_UNTRACK(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->readnl);
Py_CLEAR(self->writenl); Py_CLEAR(self->writenl);
Py_CLEAR(self->decoder); Py_CLEAR(self->decoder);
if (self->buf) Py_CLEAR(self->dict);
PyMem_Free(self->buf);
if (self->weakreflist != NULL) if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self); PyObject_ClearWeakRefs((PyObject *) self);
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment