summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-01-31 23:20:26 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-01-31 23:20:26 (GMT)
commit66f9fea782d330b28fe142ac4e58faff8a6f42d1 (patch)
tree664127f167fb8d906cf7f7de7d1e1557fea0e647 /Lib
parent1665a8d9313660ffdcf3eda3eb1e1cc4eefcacee (diff)
downloadcpython-66f9fea782d330b28fe142ac4e58faff8a6f42d1.zip
cpython-66f9fea782d330b28fe142ac4e58faff8a6f42d1.tar.gz
cpython-66f9fea782d330b28fe142ac4e58faff8a6f42d1.tar.bz2
Merged revisions 77895-77896 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r77895 | antoine.pitrou | 2010-01-31 23:47:27 +0100 (dim., 31 janv. 2010) | 12 lines Merged revisions 77890 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77890 | antoine.pitrou | 2010-01-31 23:26:04 +0100 (dim., 31 janv. 2010) | 7 lines - 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. ........ ................ r77896 | antoine.pitrou | 2010-02-01 00:12:29 +0100 (lun., 01 févr. 2010) | 3 lines r77895 broke doctest. ................
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py8
-rw-r--r--Lib/doctest.py3
-rw-r--r--Lib/test/test_fileio.py11
-rw-r--r--Lib/test/test_io.py20
-rw-r--r--Lib/test/test_largefile.py4
-rw-r--r--Lib/test/test_memoryio.py5
6 files changed, 38 insertions, 13 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 2458e40..a9ff985 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -851,7 +851,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
@@ -1210,8 +1210,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:
@@ -1712,8 +1711,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:
diff --git a/Lib/doctest.py b/Lib/doctest.py
index e80e74d..d6fb504 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -247,7 +247,8 @@ class _SpoofOut(StringIO):
return result
def truncate(self, size=None):
- StringIO.truncate(self, size)
+ self.seek(size)
+ StringIO.truncate(self)
# Worst-case linear-time ellipsis matching.
def _ellipsis_match(want, got):
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index 7a9b780..f89c542 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -330,6 +330,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>
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 2f76fed..71362f0 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -229,6 +229,11 @@ class IOTest(unittest.TestCase):
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)
self.assertEqual(f.tell(), 6)
@@ -239,8 +244,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):
@@ -285,7 +291,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")
@@ -980,7 +986,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")
@@ -1366,6 +1372,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)
diff --git a/Lib/test/test_largefile.py b/Lib/test/test_largefile.py
index 7fd3e0e..1c6297a 100644
--- a/Lib/test/test_largefile.py
+++ b/Lib/test/test_largefile.py
@@ -122,14 +122,14 @@ class LargeFileTest(unittest.TestCase):
newsize -= 1
f.seek(42)
f.truncate(newsize)
- 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
# across platform; cut it waaaaay back
f.seek(0)
f.truncate(1)
- 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
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 07c80436..89c8196 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -72,7 +72,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")
@@ -120,7 +120,8 @@ class MemoryTestMixin:
self.assertEqual(memio.getvalue(), buf[:6])
self.assertEqual(memio.truncate(4), 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()