mirror of
https://github.com/python/cpython.git
synced 2025-08-28 12:45:07 +00:00
Fix Issue5468 - urlencode to handle bytes and other alternate encodings.
(Extensive tests provided). Patch by Dan Mahn.
This commit is contained in:
parent
85029334f6
commit
df022da3d8
4 changed files with 165 additions and 24 deletions
|
@ -559,7 +559,7 @@ def quote_from_bytes(bs, safe='/'):
|
|||
_safe_quoters[safe] = quoter = Quoter(safe).__getitem__
|
||||
return ''.join([quoter(char) for char in bs])
|
||||
|
||||
def urlencode(query, doseq=False):
|
||||
def urlencode(query, doseq=False, safe='', encoding=None, errors=None):
|
||||
"""Encode a sequence of two-element tuples or dictionary into a URL query string.
|
||||
|
||||
If any values in the query arg are sequences and doseq is true, each
|
||||
|
@ -568,6 +568,10 @@ def urlencode(query, doseq=False):
|
|||
If the query arg is a sequence of two-element tuples, the order of the
|
||||
parameters in the output will match the order of parameters in the
|
||||
input.
|
||||
|
||||
The query arg may be either a string or a bytes type. When query arg is a
|
||||
string, the safe, encoding and error parameters are sent the quote_plus for
|
||||
encoding.
|
||||
"""
|
||||
|
||||
if hasattr(query, "items"):
|
||||
|
@ -592,14 +596,28 @@ def urlencode(query, doseq=False):
|
|||
l = []
|
||||
if not doseq:
|
||||
for k, v in query:
|
||||
k = quote_plus(str(k))
|
||||
v = quote_plus(str(v))
|
||||
if isinstance(k, bytes):
|
||||
k = quote_plus(k, safe)
|
||||
else:
|
||||
k = quote_plus(str(k), safe, encoding, errors)
|
||||
|
||||
if isinstance(v, bytes):
|
||||
v = quote_plus(v, safe)
|
||||
else:
|
||||
v = quote_plus(str(v), safe, encoding, errors)
|
||||
l.append(k + '=' + v)
|
||||
else:
|
||||
for k, v in query:
|
||||
k = quote_plus(str(k))
|
||||
if isinstance(v, str):
|
||||
v = quote_plus(v)
|
||||
if isinstance(k, bytes):
|
||||
k = quote_plus(k, safe)
|
||||
else:
|
||||
k = quote_plus(str(k), safe, encoding, errors)
|
||||
|
||||
if isinstance(v, bytes):
|
||||
v = quote_plus(v, safe)
|
||||
l.append(k + '=' + v)
|
||||
elif isinstance(v, str):
|
||||
v = quote_plus(v, safe, encoding, errors)
|
||||
l.append(k + '=' + v)
|
||||
else:
|
||||
try:
|
||||
|
@ -607,12 +625,16 @@ def urlencode(query, doseq=False):
|
|||
x = len(v)
|
||||
except TypeError:
|
||||
# not a sequence
|
||||
v = quote_plus(str(v))
|
||||
v = quote_plus(str(v), safe, encoding, errors)
|
||||
l.append(k + '=' + v)
|
||||
else:
|
||||
# loop over the sequence
|
||||
for elt in v:
|
||||
l.append(k + '=' + quote_plus(str(elt)))
|
||||
if isinstance(elt, bytes):
|
||||
elt = quote_plus(elt, safe)
|
||||
else:
|
||||
elt = quote_plus(str(elt), safe, encoding, errors)
|
||||
l.append(k + '=' + elt)
|
||||
return '&'.join(l)
|
||||
|
||||
# Utilities to parse URLs (most of these return None for missing parts):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue