[wwwsearch-commits] r26278 - in wwwsearch/mechanize/branch/mechanize-0.1.0-devel: . mechanize

jjlee at codespeak.net jjlee at codespeak.net
Tue Apr 25 00:17:30 CEST 2006


Author: jjlee
Date: Tue Apr 25 00:17:29 2006
New Revision: 26278

Modified:
   wwwsearch/mechanize/branch/mechanize-0.1.0-devel/mechanize/_mechanize.py
   wwwsearch/mechanize/branch/mechanize-0.1.0-devel/test.py
Log:
Work around problem that not all responses Browser has to deal with are instances of ClientCookie._Util.closeable_response

Modified: wwwsearch/mechanize/branch/mechanize-0.1.0-devel/mechanize/_mechanize.py
==============================================================================
--- wwwsearch/mechanize/branch/mechanize-0.1.0-devel/mechanize/_mechanize.py	(original)
+++ wwwsearch/mechanize/branch/mechanize-0.1.0-devel/mechanize/_mechanize.py	Tue Apr 25 00:17:29 2006
@@ -193,10 +193,42 @@
         return copy.copy(self._response)
 
     def set_response(self, response):
-        """Replace current response with response."""
+        """Replace current response with (a copy of) response."""
+        from ClientCookie._Util import closeable_response
+        # sanity check, necessary but far from sufficient
+        if not (hasattr(response, "info") and hasattr(response, "geturl") and
+                hasattr(response, "read")):
+            raise ValueError("not a response object")
+
         self.form = None
+
+        # XXX bleah!!
+
+        if not hasattr(response, 'closeable_response'):
+            # we expect to get here if a urllib2 handler constructed the
+            # response, i.e. the response is an urllib.addinfourl, instead of a
+            # ClientCookie._Util.closeable_response as returned by
+            # e.g. ClientCookie.HTTPHandler
+            try:
+                code = response.code
+            except AttributeError:
+                code = None
+            try:
+                msg = response.msg
+            except AttributeError:
+                msg = None
+            # assume response has an .fp attribute, the socket fileobject
+            # (i.e. is an urllib.addinfourl, really).
+            response = closeable_response(
+                response.fp, response.info(), response.geturl(), code, msg)
         if not hasattr(response, "seek"):
             response = ClientCookie.response_seek_wrapper(response)
+        # 0) don't want to copy here, but
+        # 1) don't want to copy some of the time and not other times
+        # 2) need response to be .close()able and .seek()able
+        # 3) 2) and 1) imply must always be copy.copy()ed
+        response = copy.copy(response)
+
         self._response = response
         self._factory.set_response(self._response)
 

Modified: wwwsearch/mechanize/branch/mechanize-0.1.0-devel/test.py
==============================================================================
--- wwwsearch/mechanize/branch/mechanize-0.1.0-devel/test.py	(original)
+++ wwwsearch/mechanize/branch/mechanize-0.1.0-devel/test.py	Tue Apr 25 00:17:29 2006
@@ -116,18 +116,19 @@
         return self.data.values()
 
 class MockResponse:
+    closeable_response = None
     def __init__(self, url="http://example.com/", data=None, info=None):
         self.url = url
-        self._f = StringIO.StringIO(data)
+        self.fp = StringIO.StringIO(data)
         if info is None: info = {}
         self._info = MockHeaders(info)
         self.source = "%d%d" % (id(self), random.randint(0, sys.maxint-1))
     def info(self): return self._info
     def geturl(self): return self.url
-    def read(self, size=-1): return self._f.read(size)
+    def read(self, size=-1): return self.fp.read(size)
     def seek(self, whence):
         assert whence == 0
-        self._f.seek(0)
+        self.fp.seek(0)
     def close(self): pass
     def __getstate__(self):
         state = self.__dict__


More information about the wwwsearch-commits mailing list