Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
aad2ee01
Commit
aad2ee01
authored
Oct 14, 2019
by
Stein Karlsen
Committed by
Tal Einat
Oct 14, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32498: urllib.parse.unquote also accepts bytes (GH-7768)
parent
9cb51f4e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
3 deletions
+33
-3
Doc/library/urllib.parse.rst
Doc/library/urllib.parse.rst
+6
-1
Lib/test/test_urllib.py
Lib/test/test_urllib.py
+23
-2
Lib/urllib/parse.py
Lib/urllib/parse.py
+2
-0
Misc/NEWS.d/next/Library/2018-06-17-21-02-25.bpo-32498.La3TZz.rst
...S.d/next/Library/2018-06-17-21-02-25.bpo-32498.La3TZz.rst
+2
-0
No files found.
Doc/library/urllib.parse.rst
View file @
aad2ee01
...
...
@@ -571,7 +571,7 @@ task isn't already covered by the URL parsing functions above.
percent-encoded sequences into Unicode characters, as accepted by the
:meth:`bytes.decode` method.
*string* m
ust be a :class:`str
`.
*string* m
ay be either a :class:`str` or a :class:`bytes
`.
*encoding* defaults to ``'utf-8'``.
*errors* defaults to ``'replace'``, meaning invalid sequences are replaced
...
...
@@ -579,6 +579,11 @@ task isn't already covered by the URL parsing functions above.
Example: ``unquote('/El%20Ni%C3%B1o/')`` yields ``'/El Niño/'``.
.. versionchanged:: 3.9
*string* parameter supports bytes and str objects (previously only str).
.. function:: unquote_plus(string, encoding='utf-8', errors='replace')
...
...
Lib/test/test_urllib.py
View file @
aad2ee01
...
...
@@ -1049,8 +1049,6 @@ class UnquotingTests(unittest.TestCase):
"%s" % result)
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
with support.check_warnings(('', BytesWarning), quiet=True):
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')
def test_unquoting_badpercent(self):
# Test unquoting on bad percent-escapes
...
...
@@ -1210,6 +1208,29 @@ class UnquotingTests(unittest.TestCase):
self.assertEqual(expect, result,
"using unquote(): %r != %r" % (expect, result))
def test_unquoting_with_bytes_input(self):
# ASCII characters decoded to a string
given = b'
blueberryjam
'
expect = '
blueberryjam
'
result = urllib.parse.unquote(given)
self.assertEqual(expect, result,
"using unquote(): %r != %r" % (expect, result))
# A mix of non-ASCII hex-encoded characters and ASCII characters
given = b'
bl
\
xc3
\
xa5b
\
xc3
\
xa6rsyltet
\
xc3
\
xb8y
'
expect = '
bl
\
u00e5b
\
u00e6rsyltet
\
u00f8y
'
result = urllib.parse.unquote(given)
self.assertEqual(expect, result,
"using unquote(): %r != %r" % (expect, result))
# A mix of non-ASCII percent-encoded characters and ASCII characters
given = b'
bl
%
c3
%
a5b
%
c3
%
a6rsyltet
%
c3
%
b8j
'
expect = '
bl
\
u00e5b
\
u00e6rsyltet
\
u00f8j
'
result = urllib.parse.unquote(given)
self.assertEqual(expect, result,
"using unquote(): %r != %r" % (expect, result))
class urlencode_Tests(unittest.TestCase):
"""Tests for urlencode()"""
...
...
Lib/urllib/parse.py
View file @
aad2ee01
...
...
@@ -631,6 +631,8 @@ def unquote(string, encoding='utf-8', errors='replace'):
unquote('abc%20def') -> 'abc def'.
"""
if
isinstance
(
string
,
bytes
):
return
unquote_to_bytes
(
string
).
decode
(
encoding
,
errors
)
if
'%'
not
in
string
:
string
.
split
return
string
...
...
Misc/NEWS.d/next/Library/2018-06-17-21-02-25.bpo-32498.La3TZz.rst
0 → 100644
View file @
aad2ee01
Made :func:`urllib.parse.unquote()` accept bytes in addition to strings.
Patch by Stein Karlsen.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment