diff options
author | Georg Brandl <georg@python.org> | 2007-03-13 08:14:27 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-13 08:14:27 (GMT) |
commit | ceede5c35988264ac2ca012c07a06270f992ac09 (patch) | |
tree | 81f53ea9807a67b3def61b5de702e852e869cc8e /Lib/urllib2.py | |
parent | 8f032cbb05b3eb6cc5452f96282523fb1a7a3077 (diff) | |
download | cpython-ceede5c35988264ac2ca012c07a06270f992ac09.zip cpython-ceede5c35988264ac2ca012c07a06270f992ac09.tar.gz cpython-ceede5c35988264ac2ca012c07a06270f992ac09.tar.bz2 |
Patch #1668100: urllib2 now correctly raises URLError instead of
OSError if accessing a local file via the file:// protocol fails.
Diffstat (limited to 'Lib/urllib2.py')
-rw-r--r-- | Lib/urllib2.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/Lib/urllib2.py b/Lib/urllib2.py index ba33030..046470a 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): |