From ceede5c35988264ac2ca012c07a06270f992ac09 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 13 Mar 2007 08:14:27 +0000 Subject: [PATCH] Patch #1668100: urllib2 now correctly raises URLError instead of OSError if accessing a local file via the file:// protocol fails. --- Lib/test/test_urllib2.py | 10 +++++----- Lib/urllib2.py | 30 +++++++++++++++++------------- Misc/NEWS | 3 +++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 5ca760ef4bf..a23d61e043c 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -626,11 +626,11 @@ class HandlerTests(unittest.TestCase): 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") diff --git a/Lib/urllib2.py b/Lib/urllib2.py index ba33030dce3..046470aa247 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -1214,19 +1214,23 @@ class FileHandler(BaseHandler): 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): diff --git a/Misc/NEWS b/Misc/NEWS index 4aae9196474..79d242b03a7 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -168,6 +168,9 @@ Core and builtins 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