[Python-checkins] r68367 - sandbox/trunk/io-c/_textio.c

antoine.pitrou python-checkins at python.org
Wed Jan 7 00:48:08 CET 2009


Author: antoine.pitrou
Date: Wed Jan  7 00:48:08 2009
New Revision: 68367

Log:
Add a deallocator to TextIOWrapper and make it call the close() method. Fixes test_distutils.



Modified:
   sandbox/trunk/io-c/_textio.c

Modified: sandbox/trunk/io-c/_textio.c
==============================================================================
--- sandbox/trunk/io-c/_textio.c	(original)
+++ sandbox/trunk/io-c/_textio.c	Wed Jan  7 00:48:08 2009
@@ -546,6 +546,39 @@
     return -1;
 }
 
+static void
+TextIOWrapper_dealloc(PyTextIOWrapperObject *self)
+{
+    PyObject *res;
+    /* XXX this is inelegant */
+    if (Py_TYPE(self)->tp_del == NULL) {
+        /* We need to resurrect the object as calling close() can invoke
+           arbitrary code. */
+        ((PyObject *) self)->ob_refcnt++;
+        res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
+                                          NULL);
+        if (res == NULL) {
+            /* XXX dump exception on terminal?
+               But IOBase.__del__ prefers to remain silent... */
+            PyErr_Clear();
+        }
+        Py_XDECREF(res);
+        if (--((PyObject *) self)->ob_refcnt != 0)
+            return;
+    }
+    Py_CLEAR(self->buffer);
+    Py_CLEAR(self->encoding);
+    Py_CLEAR(self->encoder);
+    Py_CLEAR(self->decoder);
+    Py_CLEAR(self->readnl);
+    Py_CLEAR(self->decoded_chars);
+    Py_CLEAR(self->snapshot);
+    if (self->weakreflist != NULL)
+        PyObject_ClearWeakRefs((PyObject *)self);
+    Py_CLEAR(self->dict);
+    Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
 Py_LOCAL_INLINE(const Py_UNICODE *)
 findchar(const Py_UNICODE *s, Py_ssize_t size, Py_UNICODE ch)
 {
@@ -1548,7 +1581,7 @@
     "TextIOWrapper",            /*tp_name*/
     sizeof(PyTextIOWrapperObject), /*tp_basicsize*/
     0,                          /*tp_itemsize*/
-    0,                          /*tp_dealloc*/
+    (destructor)TextIOWrapper_dealloc, /*tp_dealloc*/
     0,                          /*tp_print*/
     0,                          /*tp_getattr*/
     0,                          /*tp_setattr*/


More information about the Python-checkins mailing list