]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 68836 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 21 Jan 2009 01:05:40 +0000 (01:05 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 21 Jan 2009 01:05:40 +0000 (01:05 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r68836 | antoine.pitrou | 2009-01-21 01:55:13 +0100 (mer., 21 janv. 2009) | 12 lines

  Merged revisions 68835 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r68835 | antoine.pitrou | 2009-01-21 01:45:36 +0100 (mer., 21 janv. 2009) | 6 lines

    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.
  ........
................

Lib/test/test_io.py
Misc/NEWS
Modules/_fileio.c

index e8bba86ae0809e1f6abb0fa381c5006b7ca6a2aa..987fca4ea74d9f7d6ce7c0bac4250ec434f3fdee 100644 (file)
@@ -233,6 +233,17 @@ class IOTest(unittest.TestCase):
             else:
                 self.fail("1/0 didn't raise an exception")
 
+    # issue 5008
+    def test_append_mode_tell(self):
+        with io.open(support.TESTFN, "wb") as f:
+            f.write(b"xxx")
+        with io.open(support.TESTFN, "ab", buffering=0) as f:
+            self.assertEqual(f.tell(), 3)
+        with io.open(support.TESTFN, "ab") as f:
+            self.assertEqual(f.tell(), 3)
+        with io.open(support.TESTFN, "a") as f:
+            self.assert_(f.tell() > 0)
+
     def test_destructor(self):
         record = []
         class MyFileIO(io.FileIO):
index 443cffb16dfff4502344cf5f73f177e529f69a21..234c200f91aa2a38ca7a4624f094cd184cea893e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,11 @@ Core and Builtins
 Library
 -------
 
+- 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.
+
 - Issue #5013: Fixed a bug in FileHandler which occurred when the delay
   parameter was set.
 
index 22b473c513905dab6c678f1d0c8ebb829ff89917..300449349b958fe6440b71f1943f75362011476b 100644 (file)
@@ -55,6 +55,9 @@ PyTypeObject PyFileIO_Type;
 
 #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 +318,16 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
                        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: