[Python-checkins] r68753 - in python/branches/io-c/Modules: _bufferedio.c _textio.c

antoine.pitrou python-checkins at python.org
Mon Jan 19 00:13:10 CET 2009


Author: antoine.pitrou
Date: Mon Jan 19 00:13:09 2009
New Revision: 68753

Log:
Add truncate() to text IO objects



Modified:
   python/branches/io-c/Modules/_bufferedio.c
   python/branches/io-c/Modules/_textio.c

Modified: python/branches/io-c/Modules/_bufferedio.c
==============================================================================
--- python/branches/io-c/Modules/_bufferedio.c	(original)
+++ python/branches/io-c/Modules/_bufferedio.c	Mon Jan 19 00:13:09 2009
@@ -344,8 +344,6 @@
      * and a flush may be necessary to synch both views of the current
      *  file state.
      */
-    /* FIXME: raw objects don't have flush()
-     */
     res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_flush, NULL);
     if (res == NULL)
         return NULL;

Modified: python/branches/io-c/Modules/_textio.c
==============================================================================
--- python/branches/io-c/Modules/_textio.c	(original)
+++ python/branches/io-c/Modules/_textio.c	Mon Jan 19 00:13:09 2009
@@ -1952,6 +1952,33 @@
     return NULL;
 }
 
+static PyObject *
+TextIOWrapper_truncate(PyTextIOWrapperObject *self, PyObject *args)
+{
+    PyObject *pos = Py_None;
+    PyObject *res;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) {
+        return NULL;
+    }
+
+    res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_flush, NULL);
+    if (res == NULL)
+        return NULL;
+    Py_DECREF(res);
+
+    if (pos != Py_None) {
+        res = PyObject_CallMethodObjArgs((PyObject *) self,
+                                          _PyIO_str_seek, pos, NULL);
+        if (res == NULL)
+            return NULL;
+        Py_DECREF(res);
+    }
+
+    return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, NULL);
+}
+
 /* Inquiries */
 
 static PyObject *
@@ -2123,11 +2150,7 @@
 
     {"seek", (PyCFunction)TextIOWrapper_seek, METH_VARARGS},
     {"tell", (PyCFunction)TextIOWrapper_tell, METH_NOARGS},
-/*    {"truncate", (PyCFunction)TextIOWrapper_truncate, METH_VARARGS},
-    {"readinto", (PyCFunction)TextIOWrapper_readinto, METH_VARARGS},
-    {"peek", (PyCFunction)TextIOWrapper_peek, METH_VARARGS},
-    {"read1", (PyCFunction)TextIOWrapper_read1, METH_VARARGS},
-*/
+    {"truncate", (PyCFunction)TextIOWrapper_truncate, METH_VARARGS},
     {NULL, NULL}
 };
 


More information about the Python-checkins mailing list