# HG changeset patch # User Stephen Thorne # Parent e7aeaa24af7cf365ebc8873ec894737a6145fd28 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(...) diff -r e7aeaa24af7c Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Sat Jul 07 23:47:08 2012 +0200 +++ b/Lib/test/test_urllib.py Sat Jul 07 23:54:10 2012 +0200 @@ -1272,6 +1272,11 @@ request.method = 'HEAD' self.assertEqual(request.get_method(), 'HEAD') + 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 -r e7aeaa24af7c Lib/urllib/request.py --- a/Lib/urllib/request.py Sat Jul 07 23:47:08 2012 +0200 +++ b/Lib/urllib/request.py Sat Jul 07 23:54:10 2012 +0200 @@ -263,7 +263,8 @@ origin_req_host=None, unverifiable=False, method=None): # 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 -r e7aeaa24af7c Misc/NEWS --- a/Misc/NEWS Sat Jul 07 23:47:08 2012 +0200 +++ b/Misc/NEWS Sat Jul 07 23:54:10 2012 +0200 @@ -49,6 +49,10 @@ - Issue 10924: Fixed mksalt() to use a RNG that is suitable for cryptographic purpose. +- 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(...) + Extension Modules -----------------