mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +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:
|
||||
return quote(s, safe)
|
||||
|
||||
def urlencode(dict):
|
||||
"""Encode a dictionary of form entries into a URL query string."""
|
||||
def urlencode(dict,doseq=0):
|
||||
"""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 = []
|
||||
if not doseq:
|
||||
# preserve old behavior
|
||||
for k, v in dict.items():
|
||||
k = quote_plus(str(k))
|
||||
v = quote_plus(str(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)
|
||||
|
||||
# Proxy handling
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue