mirror of
https://github.com/python/cpython.git
synced 2025-07-19 17:25:54 +00:00
modify urlencode so sequences in the dict are treated as multivalued
parameters. This closes the code part of patch 103314.
This commit is contained in:
parent
a6c861fc0b
commit
a5d23a19e6
1 changed files with 36 additions and 6 deletions
|
@ -1093,13 +1093,43 @@ def quote_plus(s, safe = ''):
|
||||||
else:
|
else:
|
||||||
return quote(s, safe)
|
return quote(s, safe)
|
||||||
|
|
||||||
def urlencode(dict):
|
def urlencode(dict,doseq=0):
|
||||||
"""Encode a dictionary of form entries into a URL query string."""
|
"""Encode a dictionary of form entries into a URL query string.
|
||||||
|
|
||||||
|
If any values in the dict are sequences and doseq is true, each
|
||||||
|
sequence element is converted to a separate parameter.
|
||||||
|
"""
|
||||||
l = []
|
l = []
|
||||||
|
if not doseq:
|
||||||
|
# preserve old behavior
|
||||||
for k, v in dict.items():
|
for k, v in dict.items():
|
||||||
k = quote_plus(str(k))
|
k = quote_plus(str(k))
|
||||||
v = quote_plus(str(v))
|
v = quote_plus(str(v))
|
||||||
l.append(k + '=' + v)
|
l.append(k + '=' + v)
|
||||||
|
else:
|
||||||
|
for k, v in dict.items():
|
||||||
|
k = quote_plus(str(k))
|
||||||
|
if type(v) == types.StringType:
|
||||||
|
v = quote_plus(v)
|
||||||
|
l.append(k + '=' + v)
|
||||||
|
elif type(v) == types.UnicodeType:
|
||||||
|
# is there a reasonable way to convert to ASCII?
|
||||||
|
# encode generates a string, but "replace" or "ignore"
|
||||||
|
# lose information and "strict" can raise UnicodeError
|
||||||
|
v = quote_plus(v.encode("ASCII","replace"))
|
||||||
|
l.append(k + '=' + v)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
# is this a sufficient test for sequence-ness?
|
||||||
|
x = len(v)
|
||||||
|
except TypeError:
|
||||||
|
# not a sequence
|
||||||
|
v = quote_plus(str(v))
|
||||||
|
l.append(k + '=' + v)
|
||||||
|
else:
|
||||||
|
# loop over the sequence
|
||||||
|
for elt in v:
|
||||||
|
l.append(k + '=' + quote_plus(str(elt)))
|
||||||
return '&'.join(l)
|
return '&'.join(l)
|
||||||
|
|
||||||
# Proxy handling
|
# Proxy handling
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue