Skip to content

bpo-35100: add unquote_to_bytes_plus to the urllib.parse module #12368

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions Doc/library/urllib.parse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,18 @@ task isn't already covered by the URL parsing functions above.
Example: ``unquote_to_bytes('a%26%EF')`` yields ``b'a&\xef'``.


.. function:: unquote_to_bytes_plus(string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though other functions don't have versionadded directive I think it will be good to add one to this addition. 3.8 beta 1 has been released so I am not sure if this can be merged to 3.8.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've updated the docs with the versionadded directive set to 3.9, which seems the most logical.


Like :func:`unquote_to_bytes`, but also replace plus signs with spaces, as
required for unquoting HTML form values.

*string* must be a :class:`str`.

Example: ``unquote_to_bytes_plus('/El+Ni%C3%B1o/')`` yields ``b'/El Ni\xc3\xb1o'``.

.. versionadded:: 3.9


.. function:: urlencode(query, doseq=False, safe='', encoding=None, \
errors=None, quote_via=quote_plus)

Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,12 @@ def test_unquote_to_bytes(self):
result = urllib.parse.unquote_to_bytes('')
self.assertEqual(result, b'')

def test_unquote_to_bytes_plus(self):
result = urllib.parse.unquote_to_bytes_plus('abc%20def')
self.assertEqual(result, b'abc def')
result = urllib.parse.unquote_to_bytes_plus('abc+def')
self.assertEqual(result, b'abc def')

def test_quote_errors(self):
self.assertRaises(TypeError, urllib.parse.quote, b'foo',
encoding='utf-8')
Expand Down
14 changes: 12 additions & 2 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"urlsplit", "urlunsplit", "urlencode", "parse_qs",
"parse_qsl", "quote", "quote_plus", "quote_from_bytes",
"unquote", "unquote_plus", "unquote_to_bytes",
"DefragResult", "ParseResult", "SplitResult",
"DefragResultBytes", "ParseResultBytes", "SplitResultBytes"]
"unquote_to_bytes_plus", "DefragResult", "ParseResult",
"SplitResult", "DefragResultBytes", "ParseResultBytes",
"SplitResultBytes"]

# A classification of schemes.
# The empty string classifies URLs with no scheme specified,
Expand Down Expand Up @@ -755,6 +756,15 @@ def unquote_plus(string, encoding='utf-8', errors='replace'):
string = string.replace('+', ' ')
return unquote(string, encoding, errors)

def unquote_to_bytes_plus(string):
"""Like unquote_to_bytes(), but also replace plus signs with spaces, as
required for unquoting HTML form values.

unquote_to_bytes_plus('%7e/abc+def') -> b'~/abc def'
"""
string = string.replace('+', ' ')
return unquote_to_bytes(string)

_ALWAYS_SAFE = frozenset(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
b'abcdefghijklmnopqrstuvwxyz'
b'0123456789'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :func:`urllib.parse.unquote_to_bytes_plus`. Patch contributed by Andrew Gates.