Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac71c54

Browse files
author
Victor Stinner
committed
Add encoding and errors arguments to urllib.parse_qs() and urllib.parse_qsl()
1 parent 489f392 commit ac71c54

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

Doc/library/urllib.parse.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ or on combining URL components into a URL string.
116116
Added IPv6 URL parsing capabilities.
117117

118118

119-
.. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False)
119+
.. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
120120

121121
Parse a query string given as a string argument (data of type
122122
:mimetype:`application/x-www-form-urlencoded`). Data are returned as a
@@ -133,11 +133,15 @@ or on combining URL components into a URL string.
133133
parsing errors. If false (the default), errors are silently ignored. If true,
134134
errors raise a :exc:`ValueError` exception.
135135

136+
The optional *encoding* and *errors* parameters specify how to decode
137+
percent-encoded sequences into Unicode characters, as accepted by the
138+
:meth:`bytes.decode` method.
139+
136140
Use the :func:`urllib.parse.urlencode` function to convert such
137141
dictionaries into query strings.
138142

139143

140-
.. function:: parse_qsl(qs, keep_blank_values=False, strict_parsing=False)
144+
.. function:: parse_qsl(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
141145

142146
Parse a query string given as a string argument (data of type
143147
:mimetype:`application/x-www-form-urlencoded`). Data are returned as a list of
@@ -153,6 +157,10 @@ or on combining URL components into a URL string.
153157
parsing errors. If false (the default), errors are silently ignored. If true,
154158
errors raise a :exc:`ValueError` exception.
155159

160+
The optional *encoding* and *errors* parameters specify how to decode
161+
percent-encoded sequences into Unicode characters, as accepted by the
162+
:meth:`bytes.decode` method.
163+
156164
Use the :func:`urllib.parse.urlencode` function to convert such lists of pairs into
157165
query strings.
158166

Lib/urllib/parse.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ def unquote(string, encoding='utf-8', errors='replace'):
523523
string += pct_sequence.decode(encoding, errors)
524524
return string
525525

526-
def parse_qs(qs, keep_blank_values=False, strict_parsing=False):
526+
def parse_qs(qs, keep_blank_values=False, strict_parsing=False,
527+
encoding='utf-8', errors='replace'):
527528
"""Parse a query given as a string argument.
528529
529530
Arguments:
@@ -540,16 +541,22 @@ def parse_qs(qs, keep_blank_values=False, strict_parsing=False):
540541
strict_parsing: flag indicating what to do with parsing errors.
541542
If false (the default), errors are silently ignored.
542543
If true, errors raise a ValueError exception.
544+
545+
encoding and errors: specify how to decode percent-encoded sequences
546+
into Unicode characters, as accepted by the bytes.decode() method.
543547
"""
544548
dict = {}
545-
for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
549+
pairs = parse_qsl(qs, keep_blank_values, strict_parsing,
550+
encoding=encoding, errors=errors)
551+
for name, value in pairs:
546552
if name in dict:
547553
dict[name].append(value)
548554
else:
549555
dict[name] = [value]
550556
return dict
551557

552-
def parse_qsl(qs, keep_blank_values=False, strict_parsing=False):
558+
def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
559+
encoding='utf-8', errors='replace'):
553560
"""Parse a query given as a string argument.
554561
555562
Arguments:
@@ -566,6 +573,9 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False):
566573
false (the default), errors are silently ignored. If true,
567574
errors raise a ValueError exception.
568575
576+
encoding and errors: specify how to decode percent-encoded sequences
577+
into Unicode characters, as accepted by the bytes.decode() method.
578+
569579
Returns a list, as G-d intended.
570580
"""
571581
qs, _coerce_result = _coerce_args(qs)
@@ -584,8 +594,12 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False):
584594
else:
585595
continue
586596
if len(nv[1]) or keep_blank_values:
587-
name = _coerce_result(unquote(nv[0].replace('+', ' ')))
588-
value = _coerce_result(unquote(nv[1].replace('+', ' ')))
597+
name = nv[0].replace('+', ' ')
598+
name = unquote(name, encoding=encoding, errors=errors)
599+
name = _coerce_result(name)
600+
value = nv[1].replace('+', ' ')
601+
value = unquote(value, encoding=encoding, errors=errors)
602+
value = _coerce_result(value)
589603
r.append((name, value))
590604
return r
591605

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Add encoding and errors arguments to urllib.parse_qs() and urllib.parse_qsl()
47+
4648
- Issue #10899: No function type annotations in the standard library.
4749
Removed function type annotations from _pyio.py.
4850

0 commit comments

Comments
 (0)