diff options
author | Guido van Rossum <guido@python.org> | 2007-10-25 23:21:03 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-10-25 23:21:03 (GMT) |
commit | 79b79eeab239f97d2a1374a83799aebc11e9ae7e (patch) | |
tree | 6e2aab237c4210ee875aac288b42e16f18df9939 /Lib/io.py | |
parent | 687b9c0779d81714d8ad22157e8e0f5dfc88d904 (diff) | |
download | cpython-79b79eeab239f97d2a1374a83799aebc11e9ae7e.zip cpython-79b79eeab239f97d2a1374a83799aebc11e9ae7e.tar.gz cpython-79b79eeab239f97d2a1374a83799aebc11e9ae7e.tar.bz2 |
Patch # 1323 by Amaury Forgeot d'Arc.
This patch corrects a problem in test_file.py on Windows:
f.truncate() seeks to the truncation point, but does not empty the
buffers. In the test, f.tell() returns -1...
Diffstat (limited to 'Lib/io.py')
-rw-r--r-- | Lib/io.py | 19 |
1 files changed, 17 insertions, 2 deletions
@@ -597,9 +597,24 @@ class _BufferedIOMixin(BufferedIOBase): return self.raw.tell() def truncate(self, pos=None): + # On Windows, the truncate operation changes the current position + # to the end of the file, which may leave us with desynchronized + # buffers. + # Since we promise that truncate() won't change the current position, + # the easiest thing is to capture current pos now and seek back to + # it at the end. + + initialpos = self.tell() if pos is None: - pos = self.tell() - return self.raw.truncate(pos) + pos = initialpos + + # Flush the stream. We're mixing buffered I/O with lower-level I/O, + # and a flush may be necessary to synch both views of the current + # file state. + self.flush() + newpos = self.raw.truncate(pos) + self.seek(initialpos) + return newpos ### Flush and close ### |