[pypy-commit] pypy streamio-bufinput: added back in whence=2 code for when seek isn't implemented adapted to new buffering system. all streamio tests passed.

justinpeel noreply at buildbot.pypy.org
Sat Aug 27 07:34:07 CEST 2011


Author: Justin Peel <notmuchtotell at gmail.com>
Branch: streamio-bufinput
Changeset: r46809:5375c356fe0c
Date: 2011-08-26 23:39 -0600
http://bitbucket.org/pypy/pypy/changeset/5375c356fe0c/

Log:	added back in whence=2 code for when seek isn't implemented adapted
	to new buffering system. all streamio tests passed.

diff --git a/pypy/rlib/streamio.py b/pypy/rlib/streamio.py
--- a/pypy/rlib/streamio.py
+++ b/pypy/rlib/streamio.py
@@ -547,10 +547,41 @@
                 self.read(intoffset)
             return
         if whence == 2:
-            self.do_seek(offset, 2)
+            try:
+                self.do_seek(offset, 2)
+            except MyNotImplementedError:
+                pass
+            else:
+                self.pos = 0
+                self.buf = ""
+                return
+            # Skip relative to EOF by reading and saving only just as
+            # much as needed
+            intoffset = offset2int(offset)
+            pos = self.pos
+            assert pos >= 0
+            buffers = [self.buf[pos:]]
+            total = len(buffers[0])
+            self.buf = ""
             self.pos = 0
-            self.buf = ""
+            while 1:
+                data = self.do_read(self.bufsize)
+                if not data:
+                    break
+                buffers.append(data)
+                total += len(data)
+                while buffers and total >= len(buffers[0]) - intoffset:
+                    total -= len(buffers[0])
+                    del buffers[0]
+            cutoff = total + intoffset
+            if cutoff < 0:
+                raise StreamError("cannot seek back")
+            if buffers:
+                assert cutoff >= 0
+                buffers[0] = buffers[0][cutoff:]
+            self.buf = "".join(buffers)
             return
+
         raise StreamError("whence should be 0, 1 or 2")
 
     def readall(self):


More information about the pypy-commit mailing list