diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 625c6dc88796b6..495300ebeecd4d 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -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 @@ -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): diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index c129b0d7971d71..89bedffbc69c9b 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -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 @@ -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): @@ -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.") diff --git a/Misc/NEWS.d/next/Library/2022-06-13-05-32-29.gh-issue-74668.bArBQ1.rst b/Misc/NEWS.d/next/Library/2022-06-13-05-32-29.gh-issue-74668.bArBQ1.rst new file mode 100644 index 00000000000000..8623b2d6db3327 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-06-13-05-32-29.gh-issue-74668.bArBQ1.rst @@ -0,0 +1 @@ +Parse query string containing unicode characters passed as byte string.