diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-01-14 12:52:12 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-01-14 12:52:12 (GMT) |
commit | ac71c54b88b916e05e4279e9c4306d227f3a6dfe (patch) | |
tree | 0d341e7259e4ad46d0e2b7b0e0f2c50e31b14f87 /Lib/urllib/parse.py | |
parent | 489f392a0ed9996492d7c1c124b859160a3f13d3 (diff) | |
download | cpython-ac71c54b88b916e05e4279e9c4306d227f3a6dfe.zip cpython-ac71c54b88b916e05e4279e9c4306d227f3a6dfe.tar.gz cpython-ac71c54b88b916e05e4279e9c4306d227f3a6dfe.tar.bz2 |
Add encoding and errors arguments to urllib.parse_qs() and urllib.parse_qsl()
Diffstat (limited to 'Lib/urllib/parse.py')
-rw-r--r-- | Lib/urllib/parse.py | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 42f8193..9a3e42e 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -523,7 +523,8 @@ def unquote(string, encoding='utf-8', errors='replace'): string += pct_sequence.decode(encoding, errors) return string -def parse_qs(qs, keep_blank_values=False, strict_parsing=False): +def parse_qs(qs, keep_blank_values=False, strict_parsing=False, + encoding='utf-8', errors='replace'): """Parse a query given as a string argument. Arguments: @@ -540,16 +541,22 @@ def parse_qs(qs, keep_blank_values=False, strict_parsing=False): strict_parsing: flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception. + + encoding and errors: specify how to decode percent-encoded sequences + into Unicode characters, as accepted by the bytes.decode() method. """ dict = {} - for name, value in parse_qsl(qs, keep_blank_values, strict_parsing): + pairs = parse_qsl(qs, keep_blank_values, strict_parsing, + encoding=encoding, errors=errors) + for name, value in pairs: if name in dict: dict[name].append(value) else: dict[name] = [value] return dict -def parse_qsl(qs, keep_blank_values=False, strict_parsing=False): +def parse_qsl(qs, keep_blank_values=False, strict_parsing=False, + encoding='utf-8', errors='replace'): """Parse a query given as a string argument. Arguments: @@ -566,6 +573,9 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False): false (the default), errors are silently ignored. If true, errors raise a ValueError exception. + encoding and errors: specify how to decode percent-encoded sequences + into Unicode characters, as accepted by the bytes.decode() method. + Returns a list, as G-d intended. """ qs, _coerce_result = _coerce_args(qs) @@ -584,8 +594,12 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False): else: continue if len(nv[1]) or keep_blank_values: - name = _coerce_result(unquote(nv[0].replace('+', ' '))) - value = _coerce_result(unquote(nv[1].replace('+', ' '))) + name = nv[0].replace('+', ' ') + name = unquote(name, encoding=encoding, errors=errors) + name = _coerce_result(name) + value = nv[1].replace('+', ' ') + value = unquote(value, encoding=encoding, errors=errors) + value = _coerce_result(value) r.append((name, value)) return r |