[Python-checkins] r54326 - in python/trunk: Lib/test/test_urllib2.py Lib/urllib2.py Misc/NEWS

georg.brandl python-checkins at python.org
Tue Mar 13 09:14:31 CET 2007


Author: georg.brandl
Date: Tue Mar 13 09:14:27 2007
New Revision: 54326

Modified:
   python/trunk/Lib/test/test_urllib2.py
   python/trunk/Lib/urllib2.py
   python/trunk/Misc/NEWS
Log:
Patch #1668100: urllib2 now correctly raises URLError instead of
OSError if accessing a local file via the file:// protocol fails.


Modified: python/trunk/Lib/test/test_urllib2.py
==============================================================================
--- python/trunk/Lib/test/test_urllib2.py	(original)
+++ python/trunk/Lib/test/test_urllib2.py	Tue Mar 13 09:14:27 2007
@@ -626,11 +626,11 @@
 
         for url in [
             "file://localhost:80%s" % urlpath,
-# XXXX bug: these fail with socket.gaierror, should be URLError
-##             "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
-##                                    os.getcwd(), TESTFN),
-##             "file://somerandomhost.ontheinternet.com%s/%s" %
-##             (os.getcwd(), TESTFN),
+            "file:///file_does_not_exist.txt",
+            "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
+                                   os.getcwd(), TESTFN),
+            "file://somerandomhost.ontheinternet.com%s/%s" %
+            (os.getcwd(), TESTFN),
             ]:
             try:
                 f = open(TESTFN, "wb")

Modified: python/trunk/Lib/urllib2.py
==============================================================================
--- python/trunk/Lib/urllib2.py	(original)
+++ python/trunk/Lib/urllib2.py	Tue Mar 13 09:14:27 2007
@@ -1214,19 +1214,23 @@
         host = req.get_host()
         file = req.get_selector()
         localfile = url2pathname(file)
-        stats = os.stat(localfile)
-        size = stats.st_size
-        modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
-        mtype = mimetypes.guess_type(file)[0]
-        headers = mimetools.Message(StringIO(
-            'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
-            (mtype or 'text/plain', size, modified)))
-        if host:
-            host, port = splitport(host)
-        if not host or \
-           (not port and socket.gethostbyname(host) in self.get_names()):
-            return addinfourl(open(localfile, 'rb'),
-                              headers, 'file:'+file)
+        try:
+            stats = os.stat(localfile)
+            size = stats.st_size
+            modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
+            mtype = mimetypes.guess_type(file)[0]
+            headers = mimetools.Message(StringIO(
+                'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
+                (mtype or 'text/plain', size, modified)))
+            if host:
+                host, port = splitport(host)
+            if not host or \
+                (not port and socket.gethostbyname(host) in self.get_names()):
+                return addinfourl(open(localfile, 'rb'),
+                                  headers, 'file:'+file)
+        except OSError, msg:
+            # urllib2 users shouldn't expect OSErrors coming from urlopen()
+            raise URLError(msg)
         raise URLError('file not on local host')
 
 class FTPHandler(BaseHandler):

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Mar 13 09:14:27 2007
@@ -168,6 +168,9 @@
 Library
 -------
 
+- Patch #1668100: urllib2 now correctly raises URLError instead of
+  OSError if accessing a local file via the file:// protocol fails.
+
 - Patch #1677862: Require a space or tab after import in .pth files.
 
 - Patch #1192590: Fix pdb's "ignore" and "condition" commands so they trap


More information about the Python-checkins mailing list