[wwwsearch-commits] r21663 - in wwwsearch/ClientCookie/trunk:
ClientCookie test
jjlee at codespeak.net
jjlee at codespeak.net
Mon Jan 2 23:32:09 CET 2006
Author: jjlee
Date: Mon Jan 2 23:32:08 2006
New Revision: 21663
Modified:
wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py
wwwsearch/ClientCookie/trunk/test/test_misc.py
Log:
Fix ballsup in seek-past-end-of-cache case in seek_wrapper; Also fix invariant and remove a no-op .seek() call on the cache
Modified: wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py
==============================================================================
--- wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py (original)
+++ wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py Mon Jan 2 23:32:08 2006
@@ -1,6 +1,6 @@
"""Python backwards-compat., date/time routines, seekable file object wrapper.
- Copyright 2002-2004 John J Lee <jjl at pobox.com>
+ Copyright 2002-2006 John J Lee <jjl at pobox.com>
This code is free software; you can redistribute it and/or modify it under
the terms of the BSD License (see the file COPYING included with the
@@ -369,11 +369,19 @@
return _str2time(day, mon, yr, hr, min, sec, tz)
-
# XXX Andrew Dalke kindly sent me a similar class in response to my request on
# comp.lang.python, which I then proceeded to lose. I wrote this class
# instead, but I think he's released his code publicly since, could pinch the
# tests from it, at least...
+
+# for testing seek_wrapper invariant
+# module ipdc is here:
+# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/436834
+## try:
+## from ipdbc import ContractBase
+## except ImportError:
+## pass
+## class seek_wrapper(ContractBase):
class seek_wrapper:
"""Adds a seek method to a file object.
@@ -397,15 +405,19 @@
# instance. Seems to be some cStringIO.StringIO problem on 1.5.2 -- I
# get a StringOobject, with no readlines method.
- # Invariant: the end of the cache is always at the same place as the
- # end of the wrapped file:
- # self.wrapped.tell() == self.__cache.tell()
+ # XXX Does this work sensibly in the face of exceptions raised by .read()
+ # / .readline()??
def __init__(self, wrapped):
self.wrapped = wrapped
self.__have_readline = hasattr(self.wrapped, "readline")
self.__cache = StringIO()
+ def invariant(self):
+ # The end of the cache is always at the same place as the end of the
+ # wrapped file.
+ return self.wrapped.tell() == len(self.__cache.getvalue())
+
def __getattr__(self, name):
wrapped = self.__dict__.get("wrapped")
if wrapped:
@@ -424,6 +436,7 @@
# read everything
to_read = None
if to_read is None or to_read >= 0:
+ self.__cache.seek(0, 2)
if to_read is None:
self.__cache.write(self.wrapped.read())
else:
@@ -437,9 +450,6 @@
def read(self, size=-1):
pos = self.__cache.tell()
-
- self.__cache.seek(pos)
-
end = len(self.__cache.getvalue())
available = end - pos
Modified: wwwsearch/ClientCookie/trunk/test/test_misc.py
==============================================================================
--- wwwsearch/ClientCookie/trunk/test/test_misc.py (original)
+++ wwwsearch/ClientCookie/trunk/test/test_misc.py Mon Jan 2 23:32:08 2006
@@ -31,6 +31,7 @@
except ImportError:
from ClientCookie._ClientCookie import StopIteration
from ClientCookie._Util import seek_wrapper
+ from ClientCookie._Debug import warn
text = """\
The quick brown fox
jumps over the lazy
@@ -106,6 +107,14 @@
else:
assert False, "StopIteration not raised"
+ fh = TestUnSeekable(text)
+ sfh = seek_wrapper(fh)
+ sfh.read(10)
+ sfh.seek(5)
+ self.assert_(sfh.invariant())
+ sfh.seek(0, 2)
+ self.assert_(sfh.invariant())
+
if __name__ == "__main__":
import unittest
More information about the wwwsearch-commits
mailing list