Commit c0f2d2d3 authored by Guido van Rossum's avatar Guido van Rossum

SF patch# 1762940 by Joe Gregorio.

Fix test_cookielib and test_urllib2.
(The changes to urllib make urllib.quote() work correctly for Unicode
strings; but they don't fix test_urllib.)
parent 15863ea0
...@@ -644,8 +644,6 @@ def escape_path(path): ...@@ -644,8 +644,6 @@ def escape_path(path):
# And here, kind of: draft-fielding-uri-rfc2396bis-03 # And here, kind of: draft-fielding-uri-rfc2396bis-03
# (And in draft IRI specification: draft-duerst-iri-05) # (And in draft IRI specification: draft-duerst-iri-05)
# (And here, for new URI schemes: RFC 2718) # (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 = urllib.quote(path, HTTP_PATH_SAFE)
path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path) path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path)
return path return path
......
...@@ -1003,7 +1003,7 @@ class HandlerTests(unittest.TestCase): ...@@ -1003,7 +1003,7 @@ class HandlerTests(unittest.TestCase):
self.assertEqual(len(http_handler.requests), 2) self.assertEqual(len(http_handler.requests), 2)
self.assertFalse(http_handler.requests[0].has_header(auth_header)) self.assertFalse(http_handler.requests[0].has_header(auth_header))
userpass = '%s:%s' % (user, password) 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), self.assertEqual(http_handler.requests[1].get_header(auth_header),
auth_hdr_value) auth_hdr_value)
......
...@@ -1133,7 +1133,23 @@ def unquote_plus(s): ...@@ -1133,7 +1133,23 @@ def unquote_plus(s):
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz' 'abcdefghijklmnopqrstuvwxyz'
'0123456789' '_.-') '0123456789' '_.-')
_safemaps = {} _safe_quoters= {}
class Quoter:
def __init__(self, safe):
self.cache = {}
self.safe = safe + always_safe
def __call__(self, c):
try:
return self.cache[c]
except KeyError:
if ord(c) < 256:
res = (c in self.safe) and c or ('%%%02X' % ord(c))
self.cache[c] = res
return res
else:
return "".join(['%%%02X' % i for i in c.encode("utf-8")])
def quote(s, safe = '/'): def quote(s, safe = '/'):
"""quote('abc def') -> 'abc%20def' """quote('abc def') -> 'abc%20def'
...@@ -1158,15 +1174,11 @@ def quote(s, safe = '/'): ...@@ -1158,15 +1174,11 @@ def quote(s, safe = '/'):
""" """
cachekey = (safe, always_safe) cachekey = (safe, always_safe)
try: try:
safe_map = _safemaps[cachekey] quoter = _safe_quoters[cachekey]
except KeyError: except KeyError:
safe += always_safe quoter = Quoter(safe)
safe_map = {} _safe_quoters[cachekey] = quoter
for i in range(256): res = map(quoter, s)
c = chr(i)
safe_map[c] = (c in safe) and c or ('%%%02X' % i)
_safemaps[cachekey] = safe_map
res = map(safe_map.__getitem__, s)
return ''.join(res) return ''.join(res)
def quote_plus(s, safe = ''): def quote_plus(s, safe = ''):
......
...@@ -679,7 +679,7 @@ class ProxyHandler(BaseHandler): ...@@ -679,7 +679,7 @@ class ProxyHandler(BaseHandler):
proxy_type = orig_type proxy_type = orig_type
if user and password: if user and password:
user_pass = '%s:%s' % (unquote(user), unquote(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) req.add_header('Proxy-authorization', 'Basic ' + creds)
hostport = unquote(hostport) hostport = unquote(hostport)
req.set_proxy(hostport, proxy_type) req.set_proxy(hostport, proxy_type)
...@@ -802,7 +802,7 @@ class AbstractBasicAuthHandler: ...@@ -802,7 +802,7 @@ class AbstractBasicAuthHandler:
user, pw = self.passwd.find_user_password(realm, host) user, pw = self.passwd.find_user_password(realm, host)
if pw is not None: if pw is not None:
raw = "%s:%s" % (user, pw) 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: if req.headers.get(self.auth_header, None) == auth:
return None return None
req.add_header(self.auth_header, auth) req.add_header(self.auth_header, auth)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment