File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1093,13 +1093,43 @@ def quote_plus(s, safe = ''):
10931093 else :
10941094 return quote (s , safe )
10951095
1096- def urlencode (dict ):
1097- """Encode a dictionary of form entries into a URL query string."""
1096+ def urlencode (dict ,doseq = 0 ):
1097+ """Encode a dictionary of form entries into a URL query string.
1098+
1099+ If any values in the dict are sequences and doseq is true, each
1100+ sequence element is converted to a separate parameter.
1101+ """
10981102 l = []
1099- for k , v in dict .items ():
1100- k = quote_plus (str (k ))
1101- v = quote_plus (str (v ))
1102- l .append (k + '=' + v )
1103+ if not doseq :
1104+ # preserve old behavior
1105+ for k , v in dict .items ():
1106+ k = quote_plus (str (k ))
1107+ v = quote_plus (str (v ))
1108+ l .append (k + '=' + v )
1109+ else :
1110+ for k , v in dict .items ():
1111+ k = quote_plus (str (k ))
1112+ if type (v ) == types .StringType :
1113+ v = quote_plus (v )
1114+ l .append (k + '=' + v )
1115+ elif type (v ) == types .UnicodeType :
1116+ # is there a reasonable way to convert to ASCII?
1117+ # encode generates a string, but "replace" or "ignore"
1118+ # lose information and "strict" can raise UnicodeError
1119+ v = quote_plus (v .encode ("ASCII" ,"replace" ))
1120+ l .append (k + '=' + v )
1121+ else :
1122+ try :
1123+ # is this a sufficient test for sequence-ness?
1124+ x = len (v )
1125+ except TypeError :
1126+ # not a sequence
1127+ v = quote_plus (str (v ))
1128+ l .append (k + '=' + v )
1129+ else :
1130+ # loop over the sequence
1131+ for elt in v :
1132+ l .append (k + '=' + quote_plus (str (elt )))
11031133 return '&' .join (l )
11041134
11051135# Proxy handling
You can’t perform that action at this time.
0 commit comments