]> granicus.if.org Git - python/commitdiff
If append mode is specified seek to the end of the file.
authorWalter Dörwald <walter@livinglogic.de>
Wed, 6 Jun 2007 16:31:14 +0000 (16:31 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Wed, 6 Jun 2007 16:31:14 +0000 (16:31 +0000)
Add a test to test_fileio.py for this.

Lib/test/test_fileio.py
Modules/_fileio.c

index 064ec0caad8e2dc2f9ca82282c3a9ce4fd0aaeb2..8cf79df370ea4acd48b38f8675b843ffa9dae025 100644 (file)
@@ -205,6 +205,24 @@ class OtherFileTests(unittest.TestCase):
         finally:
             os.unlink(TESTFN)
 
+    def testAppend(self):
+        try:
+            f = open(TESTFN, 'wb')
+            f.write(b'spam')
+            f.close()
+            f = open(TESTFN, 'ab')
+            f.write(b'eggs')
+            f.close()
+            f = open(TESTFN, 'rb')
+            d = f.read()
+            f.close()
+            self.assertEqual(d, b'spameggs')
+        finally:
+            try:
+                os.unlink(TESTFN)
+            except:
+                pass
+
 def test_main():
     # Historically, these tests have been sloppy about removing TESTFN.
     # So get rid of it no matter what.
index c46f17e2b0e697c4005e66889345d826c616e766..364748adee6566e893023604a105045760afb284 100644 (file)
@@ -242,6 +242,18 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
                        PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
                        goto error;
                }
+               if (append) {
+                       int result;
+                       Py_BEGIN_ALLOW_THREADS
+                       errno = 0;
+                       result = lseek(self->fd, 0, SEEK_END);
+                       Py_END_ALLOW_THREADS
+                       if (result < 0) {
+                               close(self->fd);
+                               PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
+                               goto error;
+                       }
+               }
        }
 
        goto done;