Index: Lib/cookielib.py =================================================================== --- Lib/cookielib.py (revision 56608) +++ Lib/cookielib.py (working copy) @@ -644,8 +644,6 @@ # And here, kind of: draft-fielding-uri-rfc2396bis-03 # (And in draft IRI specification: draft-duerst-iri-05) # (And here, for new URI schemes: RFC 2718) - if isinstance(path, str): - path = path.encode("utf-8") path = urllib.quote(path, HTTP_PATH_SAFE) path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path) return path Index: Lib/urllib.py =================================================================== --- Lib/urllib.py (revision 56608) +++ Lib/urllib.py (working copy) @@ -1135,6 +1135,29 @@ '0123456789' '_.-') _safemaps = {} +class Memoize: + def __init__(self, fn): + self.cache = {} + self.fn = fn + + def __call__(self, arg): + try: + return self.cache[arg] + except KeyError: + res = self.fn(arg) + if ord(arg) < 256: + self.cache[arg] = res + return res + +def quote_char_builder(safe='/'): + safe = safe + always_safe + def quote_char(c, safe=safe): + if ord(c) < 256: + return (c in safe) and c or ('%%%02X' % ord(c)) + else: + return "".join(['%%%02X' % i for i in c.encode("utf-8")]) + return Memoize(quote_char) + def quote(s, safe = '/'): """quote('abc def') -> 'abc%20def' @@ -1160,13 +1183,9 @@ try: safe_map = _safemaps[cachekey] except KeyError: - safe += always_safe - safe_map = {} - for i in range(256): - c = chr(i) - safe_map[c] = (c in safe) and c or ('%%%02X' % i) + safe_map = quote_char_builder(safe) _safemaps[cachekey] = safe_map - res = map(safe_map.__getitem__, s) + res = map(safe_map, s) return ''.join(res) def quote_plus(s, safe = ''): Index: Lib/urllib2.py =================================================================== --- Lib/urllib2.py (revision 56608) +++ Lib/urllib2.py (working copy) @@ -679,7 +679,7 @@ proxy_type = orig_type if user and password: user_pass = '%s:%s' % (unquote(user), unquote(password)) - creds = base64.b64encode(user_pass).strip() + creds = str(base64.b64encode(user_pass)).strip() req.add_header('Proxy-authorization', 'Basic ' + creds) hostport = unquote(hostport) req.set_proxy(hostport, proxy_type) @@ -802,7 +802,7 @@ user, pw = self.passwd.find_user_password(realm, host) if pw is not None: raw = "%s:%s" % (user, pw) - auth = 'Basic %s' % base64.b64encode(raw).strip() + auth = 'Basic %s' % str(base64.b64encode(raw)).strip() if req.headers.get(self.auth_header, None) == auth: return None req.add_header(self.auth_header, auth) Index: Lib/test/test_urllib2.py =================================================================== --- Lib/test/test_urllib2.py (revision 56608) +++ Lib/test/test_urllib2.py (working copy) @@ -1003,7 +1003,7 @@ self.assertEqual(len(http_handler.requests), 2) self.assertFalse(http_handler.requests[0].has_header(auth_header)) userpass = '%s:%s' % (user, password) - auth_hdr_value = 'Basic '+base64.encodestring(userpass).strip() + auth_hdr_value = 'Basic ' + str(base64.encodestring(userpass)).strip() self.assertEqual(http_handler.requests[1].get_header(auth_header), auth_hdr_value)