Manual py3k backport: [svn r74155] Issue #6242: Fix deallocator of io.StringIO and io.BytesIO

This commit is contained in:
Antoine Pitrou 2009-10-24 11:59:41 +00:00
parent 5f029ce664
commit f98a267be3
3 changed files with 17 additions and 6 deletions

View File

@ -345,6 +345,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):

View File

@ -616,11 +616,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);
} }
@ -674,7 +677,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;
} }
@ -682,8 +684,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;
} }

View File

@ -515,11 +515,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);