Index: Modules/_fileio.c =================================================================== --- Modules/_fileio.c (révision 68832) +++ Modules/_fileio.c (copie de travail) @@ -55,6 +55,8 @@ #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) +static PyObject *portable_lseek(int fd, PyObject *posobj, int whence); + /* Returns 0 on success, -1 with exception set on failure. */ static int internal_close(PyFileIOObject *self) @@ -315,6 +317,13 @@ goto error; } + if (append) { + PyObject *pos = portable_lseek(self->fd, NULL, SEEK_END); + if (pos == NULL) + goto error; + Py_DECREF(pos); + } + goto done; error: Index: Lib/test/test_io.py =================================================================== --- Lib/test/test_io.py (révision 68832) +++ Lib/test/test_io.py (copie de travail) @@ -233,6 +233,17 @@ else: self.fail("1/0 didn't raise an exception") + # issue 5008 + def test_append(self): + with open(support.TESTFN, "wb") as f: + f.write(b"xxx") + with open(support.TESTFN, "ab") as f: + self.assertEqual(f.tell(), 3) + with open(support.TESTFN, "ab", buffering=0) as f: + self.assertEqual(f.tell(), 3) + with open(support.TESTFN, "a") as f: + self.assert_(f.tell() > 0) + def test_destructor(self): record = [] class MyFileIO(io.FileIO):