]> granicus.if.org Git - python/commitdiff
Fix issue14826 - make urllib.request.Request quoted url consistent with URLOpener...
authorSenthil Kumaran <senthil@uthcode.com>
Sun, 8 Jul 2012 00:37:53 +0000 (17:37 -0700)
committerSenthil Kumaran <senthil@uthcode.com>
Sun, 8 Jul 2012 00:37:53 +0000 (17:37 -0700)
Patch contributed by Stephen Thorne.

Lib/test/test_urllib2.py
Lib/urllib2.py
Misc/NEWS

index fc76a6ccb923591d6240f0bd9a5e2105c0399ab7..dd5ff8d8a5d328073ac6c26826b945b15a91bbc1 100644 (file)
@@ -1325,6 +1325,12 @@ class RequestTests(unittest.TestCase):
         req = Request("<URL:http://www.python.org>")
         self.assertEqual("www.python.org", req.get_host())
 
+    def test_quoted_full_url(self):
+        Request = urllib2.Request
+        request = Request('http://www.python.org/foo bar')
+        self.assertEqual(request.get_full_url(),
+                'http://www.python.org/foo%20bar')
+
     def test_url_fragment(self):
         req = Request("http://www.python.org/?qs=query#fragment=true")
         self.assertEqual("/?qs=query", req.get_selector())
index d4596cdf57746bf6778af1d5be570b12d848e849..944f6223b84d656b5efd285845fbdc9e16a1b4ad 100644 (file)
@@ -110,7 +110,7 @@ except ImportError:
     from StringIO import StringIO
 
 from urllib import (unwrap, unquote, splittype, splithost, quote,
-     addinfourl, splitport, splittag,
+     addinfourl, splitport, splittag, toBytes,
      splitattr, ftpwrapper, splituser, splitpasswd, splitvalue)
 
 # support for FileHandler, proxies via environment variables
@@ -196,7 +196,8 @@ class Request:
     def __init__(self, url, data=None, headers={},
                  origin_req_host=None, unverifiable=False):
         # unwrap('<URL:type://host/path>') --> 'type://host/path'
-        self.__original = unwrap(url)
+        self.__original = unwrap(toBytes(url))
+        self.__original = quote(self.__original, safe="%/:=&?~#+!$,;'@()*[]|")
         self.__original, self.__fragment = splittag(self.__original)
         self.type = None
         # self.__r_type is what's left after doing the splittype
index c5e2e92737bbde2720f4b8ac78c1fa748bc8a5a6..7e6dadd73ac18bb982f118c6dff457b3517c84b5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #14826: Quote urls in urllib2.Request identically to how they
+  are quoted by urllib.URLopener. Allows urls to spaces in them to work
+  transparently with urllib.request.urlopen(...). Patch contributed by Stephen
+  Thorne.
+
 - Issue #15247: FileIO now raises an error when given a file descriptor
   pointing to a directory.