[wwwsearch-commits] r27791 - in wwwsearch/mechanize/trunk: . mechanize test

jjlee at codespeak.net jjlee at codespeak.net
Sun May 28 16:53:46 CEST 2006


Author: jjlee
Date: Sun May 28 16:53:44 2006
New Revision: 27791

Modified:
   wwwsearch/mechanize/trunk/README.html.in
   wwwsearch/mechanize/trunk/mechanize/_clientcookie.py
   wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py
   wwwsearch/mechanize/trunk/mechanize/_util.py
   wwwsearch/mechanize/trunk/test/test_mechanize.py
   wwwsearch/mechanize/trunk/test/test_urllib2.py
Log:
More cleanup resulting from upgrade to Python 2.3

Modified: wwwsearch/mechanize/trunk/README.html.in
==============================================================================
--- wwwsearch/mechanize/trunk/README.html.in	(original)
+++ wwwsearch/mechanize/trunk/README.html.in	Sun May 28 16:53:44 2006
@@ -283,7 +283,6 @@
 <em>This is <strong>very</strong> roughly in order of priority</em>
 
 <ul>
-  <li>Add .get_method() to Request.
   <li>Can maybe switch back to using some stuff direct from urllib2
     now using 2.3 -- e.g. maybe HTTPRedirectHandler?
   <li>Test <code>.any_response()</code> two handlers case: ordering.

Modified: wwwsearch/mechanize/trunk/mechanize/_clientcookie.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_clientcookie.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_clientcookie.py	Sun May 28 16:53:44 2006
@@ -46,7 +46,7 @@
 DEFAULT_HTTP_PORT = str(httplib.HTTP_PORT)
 
 from _headersutil import split_header_words, parse_ns_headers
-from _util import startswith, endswith, isstringlike, getheaders
+from _util import startswith, endswith, isstringlike
 
 debug = logging.getLogger("mechanize.cookies").debug
 
@@ -1365,8 +1365,8 @@
         """
         # get cookie-attributes for RFC 2965 and Netscape protocols
         headers = response.info()
-        rfc2965_hdrs = getheaders(headers, "Set-Cookie2")
-        ns_hdrs = getheaders(headers, "Set-Cookie")
+        rfc2965_hdrs = headers.getheaders("Set-Cookie2")
+        ns_hdrs = headers.getheaders("Set-Cookie")
 
         rfc2965 = self._policy.rfc2965
         netscape = self._policy.netscape

Modified: wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py	Sun May 28 16:53:44 2006
@@ -23,7 +23,7 @@
 import _opener
 from _request import Request
 from _util import isstringlike, startswith, \
-     getheaders, closeable_response, response_seek_wrapper
+     closeable_response, response_seek_wrapper
 from _html import unescape, unescape_charref
 from _headersutil import is_html
 from _clientcookie import CookieJar, request_host
@@ -35,9 +35,8 @@
 DEFAULT_ENCODING = 'latin-1'
 
 
-# This fixes a bug in urllib2 as of Python 2.1.3 and 2.2.2
-#  (http://www.python.org/sf/549151)
-# 2.2.3 is broken here (my fault!), 2.3 is fixed.
+# This adds "refresh" to the list of redirectables and provides a redirection
+# algorithm that doesn't go into a loop in the presence of cookies.
 class HTTPRedirectHandler(BaseHandler):
     # maximum number of redirections to any single URL
     # this is needed because of the state that cookies introduce
@@ -91,9 +90,9 @@
         # Some servers (incorrectly) return multiple Location headers
         # (so probably same goes for URI).  Use first header.
         if headers.has_key('location'):
-            newurl = getheaders(headers, 'location')[0]
+            newurl = headers.getheaders('location')[0]
         elif headers.has_key('uri'):
-            newurl = getheaders(headers, 'uri')[0]
+            newurl = headers.getheaders('uri')[0]
         else:
             return
         newurl = urlparse.urljoin(req.get_full_url(), newurl)
@@ -304,7 +303,7 @@
             response = response_seek_wrapper(response)
         headers = response.info()
         url = response.geturl()
-        ct_hdrs = getheaders(response.info(), "content-type")
+        ct_hdrs = response.info().getheaders("content-type")
         if is_html(ct_hdrs, url, self._allow_xhtml):
             try:
                 try:
@@ -481,7 +480,7 @@
         code, msg, hdrs = response.code, response.msg, response.info()
 
         if code == 200 and hdrs.has_key("refresh"):
-            refresh = getheaders(hdrs, "refresh")[0]
+            refresh = hdrs.getheaders("refresh")[0]
             ii = string.find(refresh, ";")
             if ii != -1:
                 pause, newurl_spec = float(refresh[:ii]), refresh[ii+1:]

Modified: wwwsearch/mechanize/trunk/mechanize/_util.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_util.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_util.py	Sun May 28 16:53:44 2006
@@ -42,33 +42,6 @@
 ##     return sys.exc_traceback.tb_frame.f_back.f_back.f_code.co_name
 
 
-# this is here rather than in _HeadersUtil as it's just for
-# compatibility with old Python versions, rather than entirely new code
-def getheaders(msg, name):
-    """Get all values for a header.
-
-    This returns a list of values for headers given more than once; each
-    value in the result list is stripped in the same way as the result of
-    getheader().  If the header is not given, return an empty list.
-    """
-    result = []
-    current = ''
-    have_header = 0
-    for s in msg.getallmatchingheaders(name):
-        if isspace(s[0]):
-            if current:
-                current = "%s\n %s" % (current, string.strip(s))
-            else:
-                current = string.strip(s)
-        else:
-            if have_header:
-                result.append(current)
-            current = string.strip(s[string.find(s, ":") + 1:])
-            have_header = 1
-    if have_header:
-        result.append(current)
-    return result
-
 from calendar import timegm
 
 # Date/time conversion routines for formats used by the HTTP protocol.

Modified: wwwsearch/mechanize/trunk/test/test_mechanize.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_mechanize.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_mechanize.py	Sun May 28 16:53:44 2006
@@ -94,11 +94,10 @@
     def __call__(self, *args):
         return apply(self.handle, (self.meth_name, self.action)+args)
 
-class MockHeaders(UserDict.UserDict):
-    def getallmatchingheaders(self, name):
-        return ["%s: %s" % (k, v) for k, v in self.data.iteritems()]
+class MockHeaders(dict):
     def getheaders(self, name):
-        return self.data.values()
+        name = name.lower()
+        return [v for k, v in self.iteritems() if name == k.lower()]
 
 class MockResponse:
     closeable_response = None

Modified: wwwsearch/mechanize/trunk/test/test_urllib2.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_urllib2.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_urllib2.py	Sun May 28 16:53:44 2006
@@ -38,13 +38,10 @@
     def readline(self, count=None): pass
     def close(self): pass
 
-class MockHeaders(UserDict.UserDict):
-    def getallmatchingheaders(self, name):
-        r = []
-        for k, v in self.data.items():
-            if k.lower() == name:
-                r.append("%s: %s" % (k, v))
-        return r
+class MockHeaders(dict):
+    def getheaders(self, name):
+        name = name.lower()
+        return [v for k, v in self.iteritems() if name == k.lower()]
 
 class MockResponse(StringIO.StringIO):
     def __init__(self, code, msg, headers, data, url=None):


More information about the wwwsearch-commits mailing list