[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.114,1.115

Skip Montanaro montanaro@users.sourceforge.net
Sun, 28 Jan 2001 13:11:15 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv22071

Modified Files:
	urllib.py 
Log Message:
allow first param urlencode to be a sequence of two-element tuples - in this
case, the order of parameters in the output matches the order of the inputs.



Index: urllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
retrieving revision 1.114
retrieving revision 1.115
diff -C2 -r1.114 -r1.115
*** urllib.py	2001/01/20 15:56:39	1.114
--- urllib.py	2001/01/28 21:11:12	1.115
***************
*** 1094,1112 ****
          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:
--- 1094,1137 ----
          return quote(s, safe)
  
! def urlencode(query,doseq=0):
!     """Encode a sequence of two-element tuples or dictionary into a URL query string.
  
!     If any values in the query arg are sequences and doseq is true, each
      sequence element is converted to a separate parameter.
+ 
+     If the query arg is a sequence of two-element tuples, the order of the
+     parameters in the output will match the order of parameters in the
+     input.
      """
+     
+     if hasattr(query,"items"):
+         # mapping objects
+         query = query.items()
+     else:
+         # it's a bother at times that strings and string-like objects are
+         # sequences...
+         try:
+             # non-sequence items should not work with len()
+             x = len(query)
+             # non-empty strings will fail this
+             if len(query) and type(query[0]) != types.TupleType:
+                 raise TypeError
+             # zero-length sequences of all types will get here and succeed,
+             # but that's a minor nit - since the original implementation
+             # allowed empty dicts that type of behavior probably should be
+             # preserved for consistency
+         except TypeError:
+             ty,va,tb = sys.exc_info()
+             raise TypeError, "not a valid non-string sequence or mapping object", tb
+ 
      l = []
      if not doseq:
          # preserve old behavior
!         for k, v in query:
              k = quote_plus(str(k))
              v = quote_plus(str(v))
              l.append(k + '=' + v)
      else:
!         for k, v in query:
              k = quote_plus(str(k))
              if type(v) == types.StringType: