[Python-checkins] r68812 - in python/branches/io-c: Lib/test/test_io.py Modules/_fileio.c
antoine.pitrou
python-checkins at python.org
Tue Jan 20 21:15:52 CET 2009
Author: antoine.pitrou
Date: Tue Jan 20 21:15:51 2009
New Revision: 68812
Log:
Add garbage collection support to FileIO objects
Modified:
python/branches/io-c/Lib/test/test_io.py
python/branches/io-c/Modules/_fileio.c
Modified: python/branches/io-c/Lib/test/test_io.py
==============================================================================
--- python/branches/io-c/Lib/test/test_io.py (original)
+++ python/branches/io-c/Lib/test/test_io.py Tue Jan 20 21:15:51 2009
@@ -7,6 +7,8 @@
import threading
import random
import unittest
+import weakref
+import gc
from itertools import chain, cycle, count
from collections import deque
from test import support
@@ -405,6 +407,21 @@
file = io.open(f.fileno(), "r", closefd=False)
self.assertEqual(file.buffer.raw.closefd, False)
+ def test_garbage_collection(self):
+ # FileIO objects are collected, and collecting them flushes
+ # all data to disk.
+ class MyFileIO(io.FileIO):
+ pass
+ f = MyFileIO(support.TESTFN, "wb")
+ f.write(b"abcxxx")
+ f.f = f
+ wr = weakref.ref(f)
+ del f
+ gc.collect()
+ self.assert_(wr() is None, wr)
+ with open(support.TESTFN, "rb") as f:
+ self.assertEqual(f.read(), b"abcxxx")
+
class MemorySeekTestMixin:
Modified: python/branches/io-c/Modules/_fileio.c
==============================================================================
--- python/branches/io-c/Modules/_fileio.c (original)
+++ python/branches/io-c/Modules/_fileio.c Tue Jan 20 21:15:51 2009
@@ -332,11 +332,26 @@
return ret;
}
+static int
+fileio_traverse(PyFileIOObject *self, visitproc visit, void *arg)
+{
+ Py_VISIT(self->dict);
+ return 0;
+}
+
+static int
+fileio_clear(PyFileIOObject *self)
+{
+ Py_CLEAR(self->dict);
+ return 0;
+}
+
static void
fileio_dealloc(PyFileIOObject *self)
{
if (_PyIOBase_finalize((PyObject *) self) < 0)
return;
+ _PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
Py_CLEAR(self->dict);
@@ -918,12 +933,13 @@
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
+ | Py_TPFLAGS_HAVE_GC, /* tp_flags */
fileio_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
+ (traverseproc)fileio_traverse, /* tp_traverse */
+ (inquiry)fileio_clear, /* tp_clear */
0, /* tp_richcompare */
- offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
+ offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
fileio_methods, /* tp_methods */
@@ -937,5 +953,5 @@
fileio_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
fileio_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyObject_GC_Del, /* tp_free */
};
More information about the Python-checkins
mailing list