diff Modules/_fileio.c @ 50956:b88cc7e2bfb4 legacy-trunk

Issue #5008: When a file is opened in append mode with the new IO library, do an explicit seek to the end of file (so that e.g. tell() returns the file size rather than 0). This is consistent with the behaviour of the traditional 2.x file object.
author Antoine Pitrou <solipsis@pitrou.net>
date Wed, 21 Jan 2009 00:45:36 +0000
parents 61f1cb16e7b4
children 0415417cfd48
line wrap: on
line diff
--- a/Modules/_fileio.c	Tue Jan 20 23:42:54 2009 +0000
+++ b/Modules/_fileio.c	Wed Jan 21 00:45:36 2009 +0000
@@ -41,6 +41,9 @@
 
 #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
 
+static PyObject *
+portable_lseek(int fd, PyObject *posobj, int whence);
+
 /* Returns 0 on success, errno (which is < 0) on failure. */
 static int
 internal_close(PyFileIOObject *self)
@@ -296,6 +299,16 @@
 			goto error;
 	}
 
+	if (append) {
+		/* For consistent behaviour, we explicitly seek to the
+		   end of file (otherwise, it might be done only on the
+		   first write()). */
+		PyObject *pos = portable_lseek(self->fd, NULL, 2);
+		if (pos == NULL)
+			goto error;
+		Py_DECREF(pos);
+	}
+
 	goto done;
 
  error: