[wwwsearch-commits] r21666 - wwwsearch/ClientCookie/trunk/ClientCookie

jjlee at codespeak.net jjlee at codespeak.net
Tue Jan 3 00:53:54 CET 2006


Author: jjlee
Date: Tue Jan  3 00:53:53 2006
New Revision: 21666

Modified:
   wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py
Log:
Tighten up .seek() logic to read from wrapped object less often

Modified: wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py
==============================================================================
--- wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py	(original)
+++ wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py	Tue Jan  3 00:53:53 2006
@@ -424,17 +424,28 @@
         return getattr(self.__class__, name)
 
     def seek(self, offset, whence=0):
+        assert whence in [0,1,2]
         # make sure we have read all data up to the point we are seeking to
         pos = self.__cache.tell()
-        if whence == 0:  # absolute
-            to_read = offset - pos
-        elif whence == 1:  # relative to current position
-            to_read = offset
-        elif whence == 2:  # relative to end of *wrapped* file
+
+        # how much data, if any, do we need to read?
+        if whence == 2:  # 2: relative to end of *wrapped* file
             # since we don't know yet where the end of that file is, we must
             # read everything
             to_read = None
-        if to_read is None or to_read >= 0:
+        else:
+            if whence == 0:  # 0: absolute
+                want = offset - pos
+            else:  # 1: relative to current position
+                want = offset
+            end = len(self.__cache.getvalue())
+            available = end - pos
+            if want <= available:
+                to_read = 0
+            else:
+                to_read = want - available
+
+        if to_read != 0:
             self.__cache.seek(0, 2)
             if to_read is None:
                 self.__cache.write(self.wrapped.read())


More information about the wwwsearch-commits mailing list