[pypy-commit] pypy merge-2.7.2: Test and fix in mixed read/write/seek/write operations

amauryfa noreply at buildbot.pypy.org
Mon Jan 23 20:11:12 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: merge-2.7.2
Changeset: r51695:bf9a09ac155b
Date: 2012-01-23 20:10 +0100
http://bitbucket.org/pypy/pypy/changeset/bf9a09ac155b/

Log:	Test and fix in mixed read/write/seek/write operations

diff --git a/pypy/module/_io/interp_bufferedio.py b/pypy/module/_io/interp_bufferedio.py
--- a/pypy/module/_io/interp_bufferedio.py
+++ b/pypy/module/_io/interp_bufferedio.py
@@ -268,6 +268,7 @@
         if pos < 0:
             raise OperationError(space.w_IOError, space.wrap(
                 "Raw stream returned invalid position"))
+        self.abs_pos = pos
         return pos
 
     def _closed(self, space):
@@ -638,7 +639,7 @@
             if size <= available:
                 for i in range(size):
                     self.buffer[self.pos + i] = data[i]
-                if self.write_end == -1:
+                if self.write_end == -1 or self.write_pos > self.pos:
                     self.write_pos = self.pos
                 self._adjust_position(self.pos + size)
                 if self.pos > self.write_end:
diff --git a/pypy/module/_io/test/test_bufferedio.py b/pypy/module/_io/test/test_bufferedio.py
--- a/pypy/module/_io/test/test_bufferedio.py
+++ b/pypy/module/_io/test/test_bufferedio.py
@@ -467,3 +467,30 @@
         f.write('xxxx')
         f.seek(0)
         assert f.read() == 'a\nbxxxx'
+
+    def test_write_rewind_write(self):
+        # Various combinations of reading / writing / seeking
+        # backwards / writing again
+        import _io, errno
+        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 = _io.BytesIO(b)
+                bufio = _io.BufferedRandom(raw, 100)
+                mutate(bufio, i, j)
+                bufio.flush()
+                expected = bytearray(b)
+                expected[j] = 2
+                expected[i] = 1
+                assert raw.getvalue() == str(expected)


More information about the pypy-commit mailing list