summaryrefslogtreecommitdiffstats
path: root/Lib/urllib.py
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2001-01-20 15:56:39 (GMT)
committerSkip Montanaro <skip@pobox.com>2001-01-20 15:56:39 (GMT)
commita5d23a19e6685f7c754e459d4442242bac8dc84d (patch)
treefad3438d360925b90d2d525fe52512598ad3ccd6 /Lib/urllib.py
parenta6c861fc0bd07e871999754597862aa43c6847bb (diff)
downloadcpython-a5d23a19e6685f7c754e459d4442242bac8dc84d.zip
cpython-a5d23a19e6685f7c754e459d4442242bac8dc84d.tar.gz
cpython-a5d23a19e6685f7c754e459d4442242bac8dc84d.tar.bz2
modify urlencode so sequences in the dict are treated as multivalued
parameters. This closes the code part of patch 103314.
Diffstat (limited to 'Lib/urllib.py')
-rw-r--r--Lib/urllib.py42
1 files changed, 36 insertions, 6 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 61fac28..f096151 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -1093,13 +1093,43 @@ def quote_plus(s, safe = ''):
else:
return quote(s, safe)
-def urlencode(dict):
- """Encode a dictionary of form entries into a URL query string."""
+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 = []
- for k, v in dict.items():
- k = quote_plus(str(k))
- v = quote_plus(str(v))
- l.append(k + '=' + v)
+ 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)
# Proxy handling