]> granicus.if.org Git - python/commitdiff
- Issue #6939: Fix file I/O objects in the `io` module to keep the original
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 31 Jan 2010 22:26:04 +0000 (22:26 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 31 Jan 2010 22:26:04 +0000 (22:26 +0000)
  file position when calling `truncate()`.  It would previously change the
  file position to the given argument, which goes against the tradition of
  ftruncate() and other truncation APIs.  Patch by Pascal Chambon.

12 files changed:
Lib/_pyio.py
Lib/test/test_fileio.py
Lib/test/test_io.py
Lib/test/test_largefile.py
Lib/test/test_memoryio.py
Misc/ACKS
Misc/NEWS
Modules/_io/bytesio.c
Modules/_io/fileio.c
Modules/_io/iobase.c
Modules/_io/stringio.c
Modules/_io/textio.c

index 49fbe19f32659a81d9b61233d73cc9a6ae4a62cf..a1c21d311cf3a3eebe224cb1b8ba822e0ac4475c 100644 (file)
@@ -867,7 +867,7 @@ class BytesIO(BufferedIOBase):
         elif pos < 0:
             raise ValueError("negative truncate position %r" % (pos,))
         del self._buffer[pos:]
-        return self.seek(pos)
+        return pos
 
     def readable(self):
         return True
@@ -1226,8 +1226,7 @@ class BufferedRandom(BufferedWriter, BufferedReader):
         if pos is None:
             pos = self.tell()
         # Use seek to flush the read buffer.
-        self.seek(pos)
-        return BufferedWriter.truncate(self)
+        return BufferedWriter.truncate(self, pos)
 
     def read(self, n=None):
         if n is None:
@@ -1727,8 +1726,7 @@ class TextIOWrapper(TextIOBase):
         self.flush()
         if pos is None:
             pos = self.tell()
-        self.seek(pos)
-        return self.buffer.truncate()
+        return self.buffer.truncate(pos)
 
     def detach(self):
         if self.buffer is None:
index 91c5251d42b92b5c3bafe911e17dfa847fe801b6..cdc31237189eac26ffa58fc43f27f0eaac676543 100644 (file)
@@ -329,6 +329,17 @@ class OtherFileTests(unittest.TestCase):
             f.close()
             self.fail("no error for invalid mode: %s" % bad_mode)
 
+    def testTruncate(self):
+        f = _FileIO(TESTFN, 'w')
+        f.write(bytes(bytearray(range(10))))
+        self.assertEqual(f.tell(), 10)
+        f.truncate(5)
+        self.assertEqual(f.tell(), 10)
+        self.assertEqual(f.seek(0, os.SEEK_END), 5)
+        f.truncate(15)
+        self.assertEqual(f.tell(), 5)
+        self.assertEqual(f.seek(0, os.SEEK_END), 15)
+
     def testTruncateOnWindows(self):
         def bug801631():
             # SF bug <http://www.python.org/sf/801631>
index 6f136f242a6145e0201c1f9fcd301e21432cf234..b91c0a128e8842b3153a021f0b1df1db6eed67f8 100644 (file)
@@ -233,6 +233,11 @@ class IOTest(unittest.TestCase):
         support.unlink(support.TESTFN)
 
     def write_ops(self, f):
+        self.assertEqual(f.write(b"blah."), 5)
+        f.truncate(0)
+        self.assertEqual(f.tell(), 5)
+        f.seek(0)
+
         self.assertEqual(f.write(b"blah."), 5)
         self.assertEqual(f.seek(0), 0)
         self.assertEqual(f.write(b"Hello."), 6)
@@ -244,8 +249,9 @@ class IOTest(unittest.TestCase):
         self.assertEqual(f.write(b"h"), 1)
         self.assertEqual(f.seek(-1, 2), 13)
         self.assertEqual(f.tell(), 13)
+
         self.assertEqual(f.truncate(12), 12)
-        self.assertEqual(f.tell(), 12)
+        self.assertEqual(f.tell(), 13)
         self.assertRaises(TypeError, f.seek, 0.0)
 
     def read_ops(self, f, buffered=False):
@@ -290,7 +296,7 @@ class IOTest(unittest.TestCase):
         self.assertEqual(f.tell(), self.LARGE + 2)
         self.assertEqual(f.seek(0, 2), self.LARGE + 2)
         self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
-        self.assertEqual(f.tell(), self.LARGE + 1)
+        self.assertEqual(f.tell(), self.LARGE + 2)
         self.assertEqual(f.seek(0, 2), self.LARGE + 1)
         self.assertEqual(f.seek(-1, 2), self.LARGE)
         self.assertEqual(f.read(2), b"x")
@@ -988,7 +994,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
             bufio = self.tp(raw, 8)
             bufio.write(b"abcdef")
             self.assertEqual(bufio.truncate(3), 3)
-            self.assertEqual(bufio.tell(), 3)
+            self.assertEqual(bufio.tell(), 6)
         with self.open(support.TESTFN, "rb", buffering=0) as f:
             self.assertEqual(f.read(), b"abc")
 
@@ -1374,6 +1380,14 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
             self.assertEqual(s,
                 b"A" + b"B" * overwrite_size + b"A" * (9 - overwrite_size))
 
+    def test_truncate_after_read_or_write(self):
+        raw = self.BytesIO(b"A" * 10)
+        bufio = self.tp(raw, 100)
+        self.assertEqual(bufio.read(2), b"AA") # the read buffer gets filled
+        self.assertEqual(bufio.truncate(), 2)
+        self.assertEqual(bufio.write(b"BB"), 2) # the write buffer increases
+        self.assertEqual(bufio.truncate(), 4)
+
     def test_misbehaved_io(self):
         BufferedReaderTest.test_misbehaved_io(self)
         BufferedWriterTest.test_misbehaved_io(self)
index c4faeada21f03f750c4c660376d56971d6d200a9..16da8d8f174738e90dedcc721139db27394ba5d7 100644 (file)
@@ -125,7 +125,7 @@ class LargeFileTest(unittest.TestCase):
             f.seek(42)
             f.truncate(newsize)
             if self.new_io:
-                self.assertEqual(f.tell(), newsize)  # else wasn't truncated
+                self.assertEqual(f.tell(), 42)
             f.seek(0, 2)
             self.assertEqual(f.tell(), newsize)
             # XXX truncate(larger than true size) is ill-defined
@@ -133,7 +133,7 @@ class LargeFileTest(unittest.TestCase):
             f.seek(0)
             f.truncate(1)
             if self.new_io:
-                self.assertEqual(f.tell(), 1)       # else pointer moved
+                self.assertEqual(f.tell(), 0)       # else pointer moved
             f.seek(0)
             self.assertEqual(len(f.read()), 1)  # else wasn't truncated
 
index c2190b689d0b19f812187ddf581ab8622db9a433..d06f1c9847f602d6c2cba69d9d8d5e97868a2472 100644 (file)
@@ -76,7 +76,7 @@ class MemoryTestMixin:
         self.assertEqual(f.seek(0), 0)
         self.assertEqual(f.write(t("h")), 1)
         self.assertEqual(f.truncate(12), 12)
-        self.assertEqual(f.tell(), 12)
+        self.assertEqual(f.tell(), 1)
 
     def test_write(self):
         buf = self.buftype("hello world\n")
@@ -127,7 +127,8 @@ class MemoryTestMixin:
         # truncate() accepts long objects
         self.assertEqual(memio.truncate(4L), 4)
         self.assertEqual(memio.getvalue(), buf[:4])
-        self.assertEqual(memio.tell(), 4)
+        self.assertEqual(memio.tell(), 6)
+        memio.seek(0, 2)
         memio.write(buf)
         self.assertEqual(memio.getvalue(), buf[:4] + buf)
         pos = memio.tell()
index 877c32648bf24808257985623d66703906798059..3eb9c493164ffc735a828204058a587609dd17f8 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -124,6 +124,7 @@ Donn Cave
 Charles Cazabon
 Per Cederqvist
 Octavian Cerna
+Pascal Chambon
 Hye-Shik Chang
 Jeffrey Chang
 Mitch Chapman
index fd99685bda46f0ea8adfb829e2a46413e7b4db91..5f64b8a8e1caca32669aa806d5cf381959cf067d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -50,6 +50,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #6939: Fix file I/O objects in the `io` module to keep the original
+  file position when calling `truncate()`.  It would previously change the
+  file position to the given argument, which goes against the tradition of
+  ftruncate() and other truncation APIs.  Patch by Pascal Chambon.
+
 - UserDict is now a new-style class.
 
 - Issue #7610: Reworked implementation of the internal
index d25cb673f58dda31864b563712f9155970ddd8ae..25ff2471b6b896905b7d944ff87c64c5ca65060b 100644 (file)
@@ -414,7 +414,7 @@ PyDoc_STRVAR(truncate_doc,
 "truncate([size]) -> int.  Truncate the file to at most size bytes.\n"
 "\n"
 "Size defaults to the current file position, as returned by tell().\n"
-"Returns the new size.  Imply an absolute seek to the position size.");
+"The current file position is unchanged.  Returns the new size.\n");
 
 static PyObject *
 bytesio_truncate(bytesio *self, PyObject *args)
@@ -453,7 +453,6 @@ bytesio_truncate(bytesio *self, PyObject *args)
         if (resize_buffer(self, size) < 0)
             return NULL;
     }
-    self->pos = size;
 
     return PyLong_FromSsize_t(size);
 }
index 164fe5bc0817bc6c9e640a6c00760538812c9d0f..59cd3f7ecf565cdbf030d61d6af1d7c4a35ea707 100644 (file)
@@ -758,8 +758,10 @@ fileio_tell(fileio *self, PyObject *args)
 static PyObject *
 fileio_truncate(fileio *self, PyObject *args)
 {
-       PyObject *posobj = NULL;
+       PyObject *posobj = NULL; /* the new size wanted by the user */
+#ifndef MS_WINDOWS
        Py_off_t pos;
+#endif
        int ret;
        int fd;
 
@@ -774,58 +776,86 @@ fileio_truncate(fileio *self, PyObject *args)
 
        if (posobj == Py_None || posobj == NULL) {
                /* Get the current position. */
-                posobj = portable_lseek(fd, NULL, 1);
-                if (posobj == NULL)
+               posobj = portable_lseek(fd, NULL, 1);
+               if (posobj == NULL)
                        return NULL;
-        }
-        else {
-               /* Move to the position to be truncated. */
-                posobj = portable_lseek(fd, posobj, 0);
-        }
-       if (posobj == NULL)
-               return NULL;
-
-#if defined(HAVE_LARGEFILE_SUPPORT)
-       pos = PyLong_AsLongLong(posobj);
-#else
-       pos = PyLong_AsLong(posobj);
-#endif
-       if (pos == -1 && PyErr_Occurred())
-               return NULL;
+       }
+       else {
+               Py_INCREF(posobj);
+       }
 
 #ifdef MS_WINDOWS
        /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
           so don't even try using it. */
        {
+               PyObject *oldposobj, *tempposobj;
                HANDLE hFile;
+       
+               /* we save the file pointer position */
+               oldposobj = portable_lseek(fd, NULL, 1); 
+               if (oldposobj == NULL) {
+                       Py_DECREF(posobj);
+                       return NULL;
+               }
+
+               /* we then move to the truncation position */
+               tempposobj = portable_lseek(fd, posobj, 0);
+               if (tempposobj == NULL) {
+                       Py_DECREF(oldposobj);
+                       Py_DECREF(posobj);
+                       return NULL;
+               }
+               Py_DECREF(tempposobj);
 
                /* Truncate.  Note that this may grow the file! */
                Py_BEGIN_ALLOW_THREADS
                errno = 0;
                hFile = (HANDLE)_get_osfhandle(fd);
-               ret = hFile == (HANDLE)-1;
+               ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
                if (ret == 0) {
                        ret = SetEndOfFile(hFile) == 0;
                        if (ret)
                                errno = EACCES;
                }
                Py_END_ALLOW_THREADS
+
+               /* we restore the file pointer position in any case */
+               tempposobj = portable_lseek(fd, oldposobj, 0);
+               Py_DECREF(oldposobj);
+               if (tempposobj == NULL) {
+                       Py_DECREF(posobj);
+                       return NULL;
+               }
+               Py_DECREF(tempposobj);
        }
 #else
+
+#if defined(HAVE_LARGEFILE_SUPPORT)
+       pos = PyLong_AsLongLong(posobj);
+#else
+       pos = PyLong_AsLong(posobj);
+#endif
+       if (PyErr_Occurred()){
+               Py_DECREF(posobj);
+               return NULL;
+       }
+
        Py_BEGIN_ALLOW_THREADS
        errno = 0;
        ret = ftruncate(fd, pos);
        Py_END_ALLOW_THREADS
+
 #endif /* !MS_WINDOWS */
 
        if (ret != 0) {
+               Py_DECREF(posobj);
                PyErr_SetFromErrno(PyExc_IOError);
                return NULL;
        }
 
        return posobj;
 }
-#endif
+#endif /* HAVE_FTRUNCATE */
 
 static char *
 mode_string(fileio *self)
index 3732f46f4a55b64cdf07f02a0f3a6f047c1fe341..33448479df6a1d8c01b50a581a4a3554704f38c2 100644 (file)
@@ -102,8 +102,8 @@ iobase_tell(PyObject *self, PyObject *args)
 PyDoc_STRVAR(iobase_truncate_doc,
     "Truncate file to size bytes.\n"
     "\n"
-    "Size defaults to the current IO position as reported by tell().  Return\n"
-    "the new size.");
+    "File pointer is left unchanged.  Size defaults to the current IO\n"
+    "position as reported by tell().  Returns the new size.");
 
 static PyObject *
 iobase_truncate(PyObject *self, PyObject *args)
index a5bfd791035ec2a10ccfd12075808eb881e49835..505bde028abb3aa42276b64bfa47c43132c095ea 100644 (file)
@@ -350,7 +350,7 @@ PyDoc_STRVAR(stringio_truncate_doc,
     "Truncate size to pos.\n"
     "\n"
     "The pos argument defaults to the current file position, as\n"
-    "returned by tell().  Imply an absolute seek to pos.\n"
+    "returned by tell().  The current file position is unchanged.\n"
     "Returns the new absolute position.\n");
 
 static PyObject *
@@ -390,7 +390,6 @@ stringio_truncate(stringio *self, PyObject *args)
             return NULL;
         self->string_size = size;
     }
-    self->pos = size;
 
     return PyLong_FromSsize_t(size);
 }
index 91fecae761f463a758b5fd9407c956078b0971d9..90a4c09f7a40c9bd1b1636a5e5fa079540df791b 100644 (file)
@@ -2313,15 +2313,7 @@ textiowrapper_truncate(textio *self, PyObject *args)
         return NULL;
     Py_DECREF(res);
 
-    if (pos != Py_None) {
-        res = PyObject_CallMethodObjArgs((PyObject *) self,
-                                          _PyIO_str_seek, pos, NULL);
-        if (res == NULL)
-            return NULL;
-        Py_DECREF(res);
-    }
-
-    return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, NULL);
+    return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, pos, NULL);
 }
 
 static PyObject *