]> granicus.if.org Git - python/commitdiff
Fixing Issue6557. urllib.urlopen will quote the space character within urls.
authorSenthil Kumaran <orsenthil@gmail.com>
Sat, 15 Aug 2009 17:49:55 +0000 (17:49 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Sat, 15 Aug 2009 17:49:55 +0000 (17:49 +0000)
Lib/test/test_urllib.py
Lib/urllib.py

index c8410d16f3604e06ae03f69899837a39ac5114ef..0870b53ea2d1d56dafb8a7de72316d851700b7a6 100644 (file)
@@ -582,6 +582,17 @@ class Pathname_Tests(unittest.TestCase):
                          "url2pathname() failed; %s != %s" %
                          (expect, result))
 
+class URLopener_Tests(unittest.TestCase):
+    """Testcase to test the open method of URLopener class."""
+    def test_quoted_open(self):
+        class DummyURLopener(urllib.URLopener):
+            def open_spam(self, url):
+                return url
+
+        self.assertEqual(DummyURLopener().open(
+            'spam://example/ /'),'//example/%20/')
+
+
 # Just commented them out.
 # Can't really tell why keep failing in windows and sparc.
 # Everywhere else they work ok, but on those machines, someteimes
@@ -676,6 +687,7 @@ def test_main():
             UnquotingTests,
             urlencode_Tests,
             Pathname_Tests,
+            URLopener_Tests,
             #FTPWrapperTests,
         )
 
index 541cec8888266ce407a9ce46f3d5048f845862ab..f69ec63692c7ba4d6558f0f81c51057cb4fd543b 100644 (file)
@@ -176,6 +176,9 @@ class URLopener:
     def open(self, fullurl, data=None):
         """Use URLopener().open(file) instead of open(file, 'r')."""
         fullurl = unwrap(toBytes(fullurl))
+        # percent encode url. fixing lame server errors like space within url
+        # parts
+        fullurl = quote(fullurl, safe="%/:=&?~#+!$,;'@()*[]")
         if self.tempcache and fullurl in self.tempcache:
             filename, headers = self.tempcache[fullurl]
             fp = open(filename, 'rb')