summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_io.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-01-31 22:47:27 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-01-31 22:47:27 (GMT)
commit905a2ffe3e5445925a669ab433521ddb66cf1043 (patch)
treef3a63e9771498b63eebb2797deaede3577695aaf /Lib/test/test_io.py
parent9b661e6ef636bdd71639befc11e7ce71755f6421 (diff)
downloadcpython-905a2ffe3e5445925a669ab433521ddb66cf1043.zip
cpython-905a2ffe3e5445925a669ab433521ddb66cf1043.tar.gz
cpython-905a2ffe3e5445925a669ab433521ddb66cf1043.tar.bz2
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. ........
Diffstat (limited to 'Lib/test/test_io.py')
-rw-r--r--Lib/test/test_io.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index fd7f404..fe1a2b8 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)