diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index c6f6f6121f3..69d543855e5 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1246,6 +1246,11 @@ class URLopener_Tests(unittest.TestCase): # ftp.close() + def test_quote_url(self): + Request = urllib.request.Request + request = Request("http://www.python.org/foo bar") + self.assertEqual(request.full_url, "http://www.python.org/foo%20bar") + def test_main(): support.run_unittest( diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index d6f9f9a1bca..796b700f99b 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -180,7 +180,8 @@ class Request: def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False): # unwrap('') --> 'type://host/path' - self.full_url = unwrap(url) + self.full_url = unwrap(to_bytes(url)) + self.full_url = quote(self.full_url, safe="%/:=&?~#+!$,;'@()*[]|") self.full_url, self.fragment = splittag(self.full_url) self.data = data self.headers = {} diff --git a/Misc/NEWS b/Misc/NEWS index 92131b4eb64..18d03efa494 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -87,6 +87,11 @@ Core and Builtins Library ------- +- Issue #14826: Quote urls in urllib.request.Request identically to how they + are quoted by urllib.request.URLopener. Allows urls to spaces in them to work + transparently with urllib.request.urlopen(...). Patch contributed by Stephen + Thorne. + - Issue #14990: Correctly fail with SyntaxError on invalid encoding declaration.