[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.113,1.114
Skip Montanaro
montanaro@users.sourceforge.net
Sat, 20 Jan 2001 07:56:41 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv22813
Modified Files:
urllib.py
Log Message:
modify urlencode so sequences in the dict are treated as multivalued
parameters. This closes the code part of patch 103314.
Index: urllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
retrieving revision 1.113
retrieving revision 1.114
diff -C2 -r1.113 -r1.114
*** urllib.py 2001/01/19 03:28:15 1.113
--- urllib.py 2001/01/20 15:56:39 1.114
***************
*** 1094,1104 ****
return quote(s, safe)
! def urlencode(dict):
! """Encode a dictionary of form entries into a URL query string."""
l = []
! for k, v in dict.items():
! k = quote_plus(str(k))
! v = quote_plus(str(v))
! l.append(k + '=' + v)
return '&'.join(l)
--- 1094,1134 ----
return quote(s, safe)
! 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)