[wwwsearch-commits] r27813 - in wwwsearch/mechanize/trunk: mechanize test

jjlee at codespeak.net jjlee at codespeak.net
Mon May 29 00:22:29 CEST 2006


Author: jjlee
Date: Mon May 29 00:22:27 2006
New Revision: 27813

Modified:
   wwwsearch/mechanize/trunk/mechanize/_clientcookie.py
   wwwsearch/mechanize/trunk/mechanize/_headersutil.py
   wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py
   wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py
   wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py
   wwwsearch/mechanize/trunk/mechanize/_opener.py
   wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py
   wwwsearch/mechanize/trunk/mechanize/_util.py
   wwwsearch/mechanize/trunk/test/test_cookies.py
   wwwsearch/mechanize/trunk/test/test_urllib2.py
Log:
Get rid of startswith / endswith compat functions

Modified: wwwsearch/mechanize/trunk/mechanize/_clientcookie.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_clientcookie.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_clientcookie.py	Mon May 29 00:22:27 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
+from _util import isstringlike
 
 debug = logging.getLogger("mechanize.cookies").debug
 
@@ -115,7 +115,7 @@
     has_form_nb = not (i == -1 or i == 0)
     return (
         has_form_nb and
-        startswith(B, ".") and
+        B.startswith(".") and
         is_HDN(B[1:])
         )
 
@@ -140,8 +140,8 @@
             # equal IP addresses
             return True
         return False
-    initial_dot = startswith(B, ".")
-    if initial_dot and endswith(A, B):
+    initial_dot = B.startswith(".")
+    if initial_dot and A.endswith(B):
         return True
     if not initial_dot and A == B:
         return True
@@ -185,7 +185,7 @@
         path = "%s;%s" % (path, parameters)
     path = escape_path(path)
     req_path = urlparse.urlunparse(("", "", path, "", query, frag))
-    if not startswith(req_path, "/"):
+    if not req_path.startswith("/"):
         # fix bad RFC 2396 absoluteURI
         req_path = "/"+req_path
     return req_path
@@ -701,7 +701,7 @@
         # Try and stop servers setting V0 cookies designed to hack other
         # servers that know both V0 and V1 protocols.
         if (cookie.version == 0 and self.strict_ns_set_initial_dollar and
-            startswith(cookie.name, "$")):
+            cookie.name.startswith("$")):
             debug("   illegal name (starts with '$'): '%s'", cookie.name)
             return False
         return True
@@ -711,7 +711,7 @@
             req_path = request_path(request)
             if ((cookie.version > 0 or
                  (cookie.version == 0 and self.strict_ns_set_path)) and
-                not startswith(req_path, cookie.path)):
+                not req_path.startswith(cookie.path)):
                 debug("   path attribute %s is not a prefix of request "
                       "path %s", cookie.path, req_path)
                 return False
@@ -757,7 +757,7 @@
         if cookie.domain_specified:
             req_host, erhn = eff_request_host(request)
             domain = cookie.domain
-            if startswith(domain, "."):
+            if domain.startswith("."):
                 undotted_domain = domain[1:]
             else:
                 undotted_domain = domain
@@ -767,9 +767,9 @@
                       domain)
                 return False
             if cookie.version == 0:
-                if (not endswith(erhn, domain) and
-                    (not startswith(erhn, ".") and
-                     not endswith("."+erhn, domain))):
+                if (not erhn.endswith(domain) and
+                    (not erhn.startswith(".") and
+                     not ("."+erhn).endswith(domain))):
                     debug("   effective request-host %s (even with added "
                           "initial dot) does not end end with %s",
                           erhn, domain)
@@ -892,7 +892,7 @@
             debug("   effective request-host name %s does not domain-match "
                   "RFC 2965 cookie domain %s", erhn, domain)
             return False
-        if cookie.version == 0 and not endswith("."+erhn, domain):
+        if cookie.version == 0 and not ("."+erhn).endswith(domain):
             debug("   request-host %s does not match Netscape cookie domain "
                   "%s", req_host, domain)
             return False
@@ -905,12 +905,12 @@
         # Munge req_host and erhn to always start with a dot, so as to err on
         # the side of letting cookies through.
         dotted_req_host, dotted_erhn = eff_request_host(request)
-        if not startswith(dotted_req_host, "."):
+        if not dotted_req_host.startswith("."):
             dotted_req_host = "."+dotted_req_host
-        if not startswith(dotted_erhn, "."):
+        if not dotted_erhn.startswith("."):
             dotted_erhn = "."+dotted_erhn
-        if not (endswith(dotted_req_host, domain) or
-                endswith(dotted_erhn, domain)):
+        if not (dotted_req_host.endswith(domain) or
+                dotted_erhn.endswith(domain)):
             #debug("   request domain %s does not match cookie domain %s",
             #      req_host, domain)
             return False
@@ -927,7 +927,7 @@
     def path_return_ok(self, path, request):
         debug("- checking cookie path=%s", path)
         req_path = request_path(request)
-        if not startswith(req_path, path):
+        if not req_path.startswith(path):
             debug("  %s does not path-match %s", req_path, path)
             return False
         return True
@@ -1096,10 +1096,10 @@
             if version > 0:
                 if cookie.path_specified:
                     attrs.append('$Path="%s"' % cookie.path)
-                if startswith(cookie.domain, "."):
+                if cookie.domain.startswith("."):
                     domain = cookie.domain
                     if (not cookie.domain_initial_dot and
-                        startswith(domain, ".")):
+                        domain.startswith(".")):
                         domain = domain[1:]
                     attrs.append('$Domain="%s"' % domain)
                 if cookie.port is not None:
@@ -1285,11 +1285,11 @@
         # but first we have to remember whether it starts with a dot
         domain_initial_dot = False
         if domain_specified:
-            domain_initial_dot = bool(startswith(domain, "."))
+            domain_initial_dot = bool(domain.startswith("."))
         if domain is Absent:
             req_host, erhn = eff_request_host(request)
             domain = erhn
-        elif not startswith(domain, "."):
+        elif not domain.startswith("."):
             domain = "."+domain
 
         # set default port

Modified: wwwsearch/mechanize/trunk/mechanize/_headersutil.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_headersutil.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_headersutil.py	Mon May 29 00:22:27 2006
@@ -14,7 +14,7 @@
 from types import UnicodeType
 STRING_TYPES = StringType, UnicodeType
 
-from _util import startswith, endswith, http2time
+from _util import http2time
 
 def is_html(ct_headers, url, allow_xhtml=False):
     """
@@ -118,7 +118,7 @@
                         # no value, a lone token
                         value = None
                 pairs.append((name, value))
-            elif startswith(text.lstrip(), ","):
+            elif text.lstrip().startswith(","):
                 # concatenated headers, as per RFC 2616 section 4.2
                 text = text.lstrip()[1:]
                 if pairs: result.append(pairs)
@@ -204,8 +204,8 @@
                     version_set = True
                 if k == "expires":
                     # convert expires date to seconds since epoch
-                    if startswith(v, '"'): v = v[1:]
-                    if endswith(v, '"'): v = v[:-1]
+                    if v.startswith('"'): v = v[1:]
+                    if v.endswith('"'): v = v[:-1]
                     v = http2time(v)  # None if invalid
             pairs.append((k, v))
 

Modified: wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py	Mon May 29 00:22:27 2006
@@ -23,7 +23,7 @@
 from _clientcookie import reraise_unmasked_exceptions, FileCookieJar, Cookie, \
      MISSING_FILENAME_TEXT, LoadError
 from _headersutil import join_header_words, split_header_words
-from _util import startswith, iso2time, time2isoz
+from _util import iso2time, time2isoz
 
 debug = logging.getLogger("mechanize").debug
 
@@ -127,7 +127,7 @@
             while 1:
                 line = f.readline()
                 if line == "": break
-                if not startswith(line, header):
+                if not line.startswith(header):
                     continue
                 line = line[len(header):].strip()
 
@@ -161,7 +161,7 @@
                     if expires is None:
                         discard = True
                     domain = h("domain")
-                    domain_specified = startswith(domain, ".")
+                    domain_specified = domain.startswith(".")
                     c = Cookie(h("version"), name, value,
                                h("port"), h("port_spec"),
                                domain, domain_specified, h("domain_dot"),

Modified: wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py	Mon May 29 00:22:27 2006
@@ -13,7 +13,6 @@
 
 from _clientcookie import reraise_unmasked_exceptions, FileCookieJar, Cookie, \
      MISSING_FILENAME_TEXT, LoadError
-from _util import startswith, endswith
 debug = logging.getLogger("ClientCookie").debug
 
 
@@ -72,11 +71,11 @@
                 if line == "": break
 
                 # last field may be absent, so keep any trailing tab
-                if endswith(line, "\n"): line = line[:-1]
+                if line.endswith("\n"): line = line[:-1]
 
                 # skip comments and blank lines XXX what is $ for?
-                if (startswith(line.strip(), "#") or
-                    startswith(line.strip(), "$") or
+                if (line.strip().startswith("#") or
+                    line.strip().startswith("$") or
                     line.strip() == ""):
                     continue
 
@@ -88,7 +87,7 @@
                     name = value
                     value = None
 
-                initial_dot = startswith(domain, ".")
+                initial_dot = domain.startswith(".")
                 assert domain_specified == initial_dot
 
                 discard = False
@@ -137,7 +136,7 @@
                     continue
                 if cookie.secure: secure = "TRUE"
                 else: secure = "FALSE"
-                if startswith(cookie.domain, "."): initial_dot = "TRUE"
+                if cookie.domain.startswith("."): initial_dot = "TRUE"
                 else: initial_dot = "FALSE"
                 if cookie.expires is not None:
                     expires = str(cookie.expires)

Modified: wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py	Mon May 29 00:22:27 2006
@@ -17,7 +17,6 @@
 
 from _clientcookie import FileCookieJar, CookieJar, Cookie, \
      MISSING_FILENAME_TEXT, LoadError
-from _util import startswith
 
 debug = logging.getLogger("mechanize").debug
 
@@ -153,7 +152,7 @@
             else:
                 discard = False
             domain = cookie["DOMAIN"]
-            initial_dot = startswith(domain, ".")
+            initial_dot = domain.startswith(".")
             if initial_dot:
                 domain_specified = True
             else:

Modified: wwwsearch/mechanize/trunk/mechanize/_opener.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_opener.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_opener.py	Mon May 29 00:22:27 2006
@@ -11,7 +11,7 @@
 
 import urllib2, bisect, urlparse
 
-from _util import startswith, isstringlike
+from _util import isstringlike
 from _request import Request
 
 try:
@@ -71,7 +71,7 @@
                 scheme = meth[:ii]
                 condition = meth[ii+1:]
 
-                if startswith(condition, "error"):
+                if condition.startswith("error"):
                     jj = meth[ii+1:].find("_") + ii + 1
                     kind = meth[jj+1:]
                     try:

Modified: wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py	Mon May 29 00:22:27 2006
@@ -22,8 +22,7 @@
 
 import _opener
 from _request import Request
-from _util import isstringlike, startswith, \
-     closeable_response, response_seek_wrapper
+from _util import isstringlike, closeable_response, response_seek_wrapper
 from _html import unescape, unescape_charref
 from _headersutil import is_html
 from _clientcookie import CookieJar, request_host

Modified: wwwsearch/mechanize/trunk/mechanize/_util.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_util.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_util.py	Mon May 29 00:22:27 2006
@@ -12,14 +12,6 @@
 from types import TupleType
 from cStringIO import StringIO
 
-def startswith(string, initial):
-    if len(initial) > len(string): return False
-    return string[:len(initial)] == initial
-
-def endswith(string, final):
-    if len(final) > len(string): return False
-    return string[-len(final):] == final
-
 def isstringlike(x):
     try: x+""
     except: return False

Modified: wwwsearch/mechanize/trunk/test/test_cookies.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_cookies.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_cookies.py	Mon May 29 00:22:27 2006
@@ -4,8 +4,6 @@
 from time import localtime
 from unittest import TestCase
 
-from mechanize._util import startswith
-
 class FakeResponse:
     def __init__(self, headers=[], url=None):
         """
@@ -962,7 +960,7 @@
         h = req.get_header("Cookie")
         assert (h.find("PART_NUMBER=ROCKET_LAUNCHER_0001") != -1 and
                 h.find("CUSTOMER=WILE_E_COYOTE") != -1 and
-                startswith(h, "SHIPPING=FEDEX;"))
+                h.startswith("SHIPPING=FEDEX;"))
 
     def test_netscape_example_2(self):
         from mechanize import CookieJar, Request

Modified: wwwsearch/mechanize/trunk/test/test_urllib2.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_urllib2.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_urllib2.py	Mon May 29 00:22:27 2006
@@ -15,7 +15,6 @@
 
 from mechanize._urllib2_support import Request, AbstractHTTPHandler, \
      build_opener, parse_head, urlopen
-from mechanize._util import startswith
 from mechanize import HTTPRedirectHandler, HTTPRequestUpgradeProcessor, \
      HTTPEquivProcessor, HTTPRefreshProcessor, SeekableProcessor, \
      HTTPCookieProcessor, HTTPRefererProcessor, \
@@ -87,7 +86,7 @@
             return res
         elif action == "return request":
             return Request("http://blah/")
-        elif startswith(action, "error"):
+        elif action.startswith("error"):
             code = int(action[-3:])
             res = MockResponse(200, "OK", {}, "")
             return self.parent.error("http", args[0], res, code, "", {})


More information about the wwwsearch-commits mailing list