]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #12062: Fix a flushing bug when doing a certain type of I/O sequence
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 12 May 2011 22:13:33 +0000 (00:13 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 12 May 2011 22:13:33 +0000 (00:13 +0200)
on a file opened in read+write mode (namely: reading, seeking a bit forward,
writing, then seeking before the previous write but still within buffered
data, and writing again).

Lib/test/test_io.py
Misc/NEWS
Modules/_io/bufferedio.c

index 5f62494e63ca41bcccdd22208668f37e74816c27..1ec6f93a0e01b833a9cbab7e0d8d30b0c1fbae4f 100644 (file)
@@ -1461,6 +1461,32 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
             self.assertEqual(s,
                 b"A" + b"B" * overwrite_size + b"A" * (9 - overwrite_size))
 
+    def test_write_rewind_write(self):
+        # Various combinations of reading / writing / seeking backwards / writing again
+        def mutate(bufio, pos1, pos2):
+            assert pos2 >= pos1
+            # Fill the buffer
+            bufio.seek(pos1)
+            bufio.read(pos2 - pos1)
+            bufio.write(b'\x02')
+            # This writes earlier than the previous write, but still inside
+            # the buffer.
+            bufio.seek(pos1)
+            bufio.write(b'\x01')
+
+        b = b"\x80\x81\x82\x83\x84"
+        for i in range(0, len(b)):
+            for j in range(i, len(b)):
+                raw = self.BytesIO(b)
+                bufio = self.tp(raw, 100)
+                mutate(bufio, i, j)
+                bufio.flush()
+                expected = bytearray(b)
+                expected[j] = 2
+                expected[i] = 1
+                self.assertEqual(raw.getvalue(), expected,
+                                 "failed result for i=%d, j=%d" % (i, j))
+
     def test_truncate_after_read_or_write(self):
         raw = self.BytesIO(b"A" * 10)
         bufio = self.tp(raw, 100)
index f065b5a99b0d680d8717e7ab79e6592cc96cd5a4..07e8a56785557d64960ffe89ca529f969a2fabba 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -69,6 +69,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #12062: Fix a flushing bug when doing a certain type of I/O sequence
+  on a file opened in read+write mode (namely: reading, seeking a bit forward,
+  writing, then seeking before the previous write but still within buffered
+  data, and writing again).
+
 - Issue #1028: Tk returns invalid Unicode null in %A: UnicodeDecodeError.
   With Tk < 8.5 _tkinter.c:PythonCmd() raised UnicodeDecodeError, caused
   IDLE to exit.  Converted to valid Unicode null in PythonCmd().
index 71c4052f29c03c32e3c130f0ece4c9b0bdbadb55..25b91e366e55e563f8aa525893cb16d373e1b54c 100644 (file)
@@ -1749,7 +1749,7 @@ bufferedwriter_write(buffered *self, PyObject *args)
     avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
     if (buf.len <= avail) {
         memcpy(self->buffer + self->pos, buf.buf, buf.len);
-        if (!VALID_WRITE_BUFFER(self)) {
+        if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
             self->write_pos = self->pos;
         }
         ADJUST_POSITION(self, self->pos + buf.len);