Commits

Brian Kearns  committed fa3dd6e

test and fix for _io buffering: in buffer back seeks shouldn't trash the buffer
just because we have no forward buffer

  • Participants
  • Parent commits df3aa2b

Comments (0)

Files changed (2)

File pypy/module/_io/interp_bufferedio.py

                 self._raw_tell(space)
             current = self.abs_pos
             available = self._readahead()
-            if available > 0:
-                if whence == 0:
-                    offset = pos - (current - self._raw_offset())
-                else:
-                    offset = pos
-                if -self.pos <= offset <= available:
-                    newpos = self.pos + offset
-                    assert newpos >= 0
-                    self.pos = newpos
-                    return space.wrap(current - available + offset)
+            if whence == 0:
+                offset = pos - (current - self._raw_offset())
+            else:
+                offset = pos
+            if -self.pos <= offset <= available:
+                newpos = self.pos + offset
+                assert newpos >= 0
+                self.pos = newpos
+                return space.wrap(current - available + offset)
 
         # Fallback: invoke raw seek() method and clear buffer
         with self.lock:

File pypy/module/_io/test/test_bufferedio.py

         assert f.read() == "\nc"
         f.close()
 
+    def test_seek_nocall(self):
+        # test that when we're at the end of the buffer,
+        # an in-buffer back seek doesn't produce a raw seek
+        import _io
+        raw = _io.FileIO(self.tmpfile)
+        f = _io.BufferedReader(raw, buffer_size=3)
+        f.read(1)
+        f.seek(3, 0)
+        def failing_seek(*args):
+            assert False
+        raw.seek = failing_seek
+        f.seek(-1, 1)
+
     def test_readlines(self):
         import _io
         raw = _io.FileIO(self.tmpfile)