@@ -135,11 +135,26 @@ def to_unicode_optional_iterator(x):
135135 assert 'is not iterable' in str (e )
136136 return x
137137 else :
138- return [ to_unicode (e ) for e in l ]
138+ return [ to_unicode (e ) for e in l ]
139+
140+ def to_utf8_optional_iterator (x ):
141+ """
142+ Raise TypeError if x is a str or if x is an iterable which
143+ contains a str.
144+ """
145+ if isinstance (x , basestring ):
146+ return to_utf8 (x )
147+
148+ try :
149+ l = list (x )
150+ except TypeError , e :
151+ assert 'is not iterable' in str (e )
152+ return x
153+ else :
154+ return [ to_utf8_if_string (e ) for e in l ]
139155
140156def escape (s ):
141157 """Escape a URL including any /."""
142- s = to_unicode (s )
143158 return urllib .quote (s .encode ('utf-8' ), safe = '~' )
144159
145160def generate_timestamp ():
@@ -386,10 +401,14 @@ def to_header(self, realm=''):
386401
387402 def to_postdata (self ):
388403 """Serialize as post data for a POST request."""
404+ d = {}
405+ for k , v in self .iteritems ():
406+ d [k .encode ('utf-8' )] = to_utf8_optional_iterator (v )
407+
389408 # tell urlencode to deal with sequence values and map them correctly
390409 # to resulting querystring. for example self["k"] = ["v1", "v2"] will
391410 # result in 'k=v1&k=v2' and not k=%5B%27v1%27%2C+%27v2%27%5D
392- return urllib .urlencode (self , True ).replace ('+' , '%20' )
411+ return urllib .urlencode (d , True ).replace ('+' , '%20' )
393412
394413 def to_url (self ):
395414 """Serialize as a URL for a GET request."""
@@ -797,7 +816,7 @@ def check(self, request, consumer, token, signature):
797816
798817class SignatureMethod_HMAC_SHA1 (SignatureMethod ):
799818 name = 'HMAC-SHA1'
800-
819+
801820 def signing_base (self , request , consumer , token ):
802821 if not hasattr (request , 'normalized_url' ) or request .normalized_url is None :
803822 raise ValueError ("Base URL for request is not set." )
0 commit comments