Index: Modules/_fileio.c =================================================================== --- Modules/_fileio.c (révision 68826) +++ Modules/_fileio.c (copie de travail) @@ -53,6 +53,8 @@ PyTypeObject PyFileIO_Type; +static PyObject *portable_lseek(int fd, PyObject *posobj, int whence); + #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) /* Returns 0 on success, -1 with exception set on failure. */ @@ -315,6 +317,17 @@ goto error; } + if (append) { + long pos; + Py_BEGIN_ALLOW_THREADS + pos = lseek(self->fd, 0, SEEK_END); + Py_END_ALLOW_THREADS + if (pos < 0) { + PyErr_SetFromErrno(PyExc_IOError); + goto error; + } + } + goto done; error: Index: Lib/test/test_io.py =================================================================== --- Lib/test/test_io.py (révision 68826) +++ 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_(0 < f.tell()) + def test_destructor(self): record = [] class MyFileIO(io.FileIO):