diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2010-07-03 17:55:41 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2010-07-03 17:55:41 (GMT) |
commit | fe1ad15b4bcc923bfba384cad4c647ece8944b83 (patch) | |
tree | 72434f3e3a6d0069f4761ddaa4185e0113b389d8 /Lib/urllib/parse.py | |
parent | 8e42fb7ada3198e66d3f060c5c87c52465a86e36 (diff) | |
download | cpython-fe1ad15b4bcc923bfba384cad4c647ece8944b83.zip cpython-fe1ad15b4bcc923bfba384cad4c647ece8944b83.tar.gz cpython-fe1ad15b4bcc923bfba384cad4c647ece8944b83.tar.bz2 |
Merged revisions 82510 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82510 | senthil.kumaran | 2010-07-03 23:18:22 +0530 (Sat, 03 Jul 2010) | 4 lines
Fix Issue5468 - urlencode to handle bytes and other alternate encodings.
(Extensive tests provided). Patch by Dan Mahn.
........
Diffstat (limited to 'Lib/urllib/parse.py')
-rw-r--r-- | Lib/urllib/parse.py | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index b7890d8..27b732b 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -533,7 +533,7 @@ def quote_from_bytes(bs, safe='/'): _safe_quoters[cachekey] = quoter return ''.join([quoter[char] for char in bs]) -def urlencode(query, doseq=False): +def urlencode(query, doseq=False, safe='', encoding=None, errors=None): """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 @@ -542,6 +542,10 @@ def urlencode(query, doseq=False): 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. + + The query arg may be either a string or a bytes type. When query arg is a + string, the safe, encoding and error parameters are sent the quote_plus for + encoding. """ if hasattr(query, "items"): @@ -566,14 +570,28 @@ def urlencode(query, doseq=False): l = [] if not doseq: for k, v in query: - k = quote_plus(str(k)) - v = quote_plus(str(v)) + if isinstance(k, bytes): + k = quote_plus(k, safe) + else: + k = quote_plus(str(k), safe, encoding, errors) + + if isinstance(v, bytes): + v = quote_plus(v, safe) + else: + v = quote_plus(str(v), safe, encoding, errors) l.append(k + '=' + v) else: for k, v in query: - k = quote_plus(str(k)) - if isinstance(v, str): - v = quote_plus(v) + if isinstance(k, bytes): + k = quote_plus(k, safe) + else: + k = quote_plus(str(k), safe, encoding, errors) + + if isinstance(v, bytes): + v = quote_plus(v, safe) + l.append(k + '=' + v) + elif isinstance(v, str): + v = quote_plus(v, safe, encoding, errors) l.append(k + '=' + v) else: try: @@ -581,12 +599,16 @@ def urlencode(query, doseq=False): x = len(v) except TypeError: # not a sequence - v = quote_plus(str(v)) + v = quote_plus(str(v), safe, encoding, errors) l.append(k + '=' + v) else: # loop over the sequence for elt in v: - l.append(k + '=' + quote_plus(str(elt))) + if isinstance(elt, bytes): + elt = quote_plus(elt, safe) + else: + elt = quote_plus(str(elt), safe, encoding, errors) + l.append(k + '=' + elt) return '&'.join(l) # Utilities to parse URLs (most of these return None for missing parts): |