Skip to content

gh-74668: Fix encoded unicode in url byte string #93757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
("a=a+b;b=b+c", [('a', 'a b;b=b c')]),
(b";a=b", [(b';a', b'b')]),
(b"a=a+b;b=b+c", [(b'a', b'a b;b=b c')]),
(b"a=a%E2%80%99b", [(b'a', b'a\xe2\x80\x99b')]),
]

# Each parse_qs testcase is a two-tuple that contains
Expand Down Expand Up @@ -66,6 +67,7 @@
("a=a+b;b=b+c", {'a': ['a b;b=b c']}),
(b";a=b", {b';a': [b'b']}),
(b"a=a+b;b=b+c", {b'a':[ b'a b;b=b c']}),
(b"a=a%E2%80%99b", {b'a': [b'a\xe2\x80\x99b']}),
]

class UrlParseTestCase(unittest.TestCase):
Expand Down
8 changes: 4 additions & 4 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _decode_args(args, encoding=_implicit_encoding,
errors=_implicit_errors):
return tuple(x.decode(encoding, errors) if x else '' for x in args)

def _coerce_args(*args):
def _coerce_args(*args, encoding=_implicit_encoding):
# Invokes decode if necessary to create str args
# and returns the coerced inputs along with
# an appropriate result coercion function
Expand All @@ -130,7 +130,7 @@ def _coerce_args(*args):
raise TypeError("Cannot mix str and non-str arguments")
if str_input:
return args + (_noop,)
return _decode_args(args) + (_encode_result,)
return _decode_args(args, encoding=encoding) + (functools.partial(_encode_result, encoding=encoding),)

# Result objects are more helpful than simple tuples
class _ResultMixinStr(object):
Expand Down Expand Up @@ -763,8 +763,8 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,

Returns a list, as G-d intended.
"""
qs, _coerce_result = _coerce_args(qs)
separator, _ = _coerce_args(separator)
qs, _coerce_result = _coerce_args(qs, encoding=encoding)
separator, _ = _coerce_args(separator, encoding=encoding)

if not separator or (not isinstance(separator, (str, bytes))):
raise ValueError("Separator must be of type string or bytes.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parse query string containing unicode characters passed as byte string.