[Python-checkins] r74462 - in python/branches/release26-maint/Lib: test/test_urllib.py urllib.py

senthil.kumaran python-checkins at python.org
Sat Aug 15 19:50:00 CEST 2009


Author: senthil.kumaran
Date: Sat Aug 15 19:49:55 2009
New Revision: 74462

Log:
Fixing Issue6557. urllib.urlopen will quote the space character within urls.



Modified:
   python/branches/release26-maint/Lib/test/test_urllib.py
   python/branches/release26-maint/Lib/urllib.py

Modified: python/branches/release26-maint/Lib/test/test_urllib.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_urllib.py	(original)
+++ python/branches/release26-maint/Lib/test/test_urllib.py	Sat Aug 15 19:49:55 2009
@@ -582,6 +582,17 @@
                          "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 @@
             UnquotingTests,
             urlencode_Tests,
             Pathname_Tests,
+            URLopener_Tests,
             #FTPWrapperTests,
         )
 

Modified: python/branches/release26-maint/Lib/urllib.py
==============================================================================
--- python/branches/release26-maint/Lib/urllib.py	(original)
+++ python/branches/release26-maint/Lib/urllib.py	Sat Aug 15 19:49:55 2009
@@ -176,6 +176,9 @@
     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')


More information about the Python-checkins mailing list