diff options
author | Antoine Pitrou <pitrou@free.fr> | 2018-01-28 17:42:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-28 17:42:31 (GMT) |
commit | 1d896ed2cddada4455f40782e4120249defbfa70 (patch) | |
tree | 622344d12b5c4ebd59fccc346103c17a928e9e1e /Lib/test | |
parent | 33febfb03998598a35f97b4ef66d33a2d43716cd (diff) | |
download | cpython-1d896ed2cddada4455f40782e4120249defbfa70.zip cpython-1d896ed2cddada4455f40782e4120249defbfa70.tar.gz cpython-1d896ed2cddada4455f40782e4120249defbfa70.tar.bz2 |
[3.6] bpo-32228: Reset raw_pos after unwinding the raw stream (GH-4858) (#5389)
Ensure that ``truncate()`` preserves the file position (as reported by ``tell()``) after writes longer than the buffer size..
(cherry picked from commit 059f58ce938d9c3f0286412a4efb1b9131339421)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_io.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 42dbbd3..6c30a44 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1703,6 +1703,23 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests): with self.open(support.TESTFN, "rb", buffering=0) as f: self.assertEqual(f.read(), b"abc") + def test_truncate_after_write(self): + # Ensure that truncate preserves the file position after + # writes longer than the buffer size. + # Issue: https://bugs.python.org/issue32228 + with self.open(support.TESTFN, "wb") as f: + # Fill with some buffer + f.write(b'\x00' * 10000) + buffer_sizes = [8192, 4096, 200] + for buffer_size in buffer_sizes: + with self.open(support.TESTFN, "r+b", buffering=buffer_size) as f: + f.write(b'\x00' * (buffer_size + 1)) + # After write write_pos and write_end are set to 0 + f.read(1) + # read operation makes sure that pos != raw_pos + f.truncate() + self.assertEqual(f.tell(), buffer_size + 2) + @unittest.skipUnless(threading, 'Threading required for this test.') @support.requires_resource('cpu') def test_threads(self): |