@@ -109,7 +109,7 @@ def _show_debug_messages():
109109except ImportError :
110110 from lib .utils import sgmllib
111111
112- import sys , types , copy , re , random
112+ import sys , re , random
113113
114114if sys .version_info >= (3 , 0 ):
115115 xrange = range
@@ -149,6 +149,14 @@ def compress_text(text): return _compress_re.sub(" ", text.strip())
149149def normalize_line_endings (text ):
150150 return re .sub (r"(?:(?<!\r)\n)|(?:\r(?!\n))" , "\r \n " , text )
151151
152+ def _quote_plus (value ):
153+ if not isinstance (value , six .string_types ):
154+ value = six .text_type (value )
155+
156+ if isinstance (value , six .text_type ):
157+ value = value .encode ("utf8" )
158+
159+ return _urllib .parse .quote_plus (value )
152160
153161# This version of urlencode is from my Python 1.5.2 back-port of the
154162# Python 2.1 CVS maintenance branch of urllib. It will accept a sequence
@@ -190,33 +198,27 @@ def urlencode(query,doseq=False,):
190198 if not doseq :
191199 # preserve old behavior
192200 for k , v in query :
193- k = _urllib . parse . quote_plus ( str ( k ) )
194- v = _urllib . parse . quote_plus ( str ( v ) )
201+ k = _quote_plus ( k )
202+ v = _quote_plus ( v )
195203 l .append (k + '=' + v )
196204 else :
197205 for k , v in query :
198- k = _urllib .parse .quote_plus (str (k ))
199- if type (v ) == types .StringType :
200- v = _urllib .parse .quote_plus (v )
201- l .append (k + '=' + v )
202- elif type (v ) == types .UnicodeType :
203- # is there a reasonable way to convert to ASCII?
204- # encode generates a string, but "replace" or "ignore"
205- # lose information and "strict" can raise UnicodeError
206- v = _urllib .parse .quote_plus (v .encode ("ASCII" ,"replace" ))
206+ k = _quote_plus (k )
207+ if isinstance (v , six .string_types ):
208+ v = _quote_plus (v )
207209 l .append (k + '=' + v )
208210 else :
209211 try :
210212 # is this a sufficient test for sequence-ness?
211213 x = len (v )
212214 except TypeError :
213215 # not a sequence
214- v = _urllib . parse . quote_plus ( str ( v ) )
216+ v = _quote_plus ( v )
215217 l .append (k + '=' + v )
216218 else :
217219 # loop over the sequence
218220 for elt in v :
219- l .append (k + '=' + _urllib . parse . quote_plus ( str ( elt ) ))
221+ l .append (k + '=' + _quote_plus ( elt ))
220222 return '&' .join (l )
221223
222224def unescape (data , entities , encoding = DEFAULT_ENCODING ):
0 commit comments