diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 58ef6255874..24296895149 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -1273,13 +1273,13 @@ class FileHandler(BaseHandler): import email.utils import mimetypes host = req.get_host() - file = req.get_selector() - localfile = url2pathname(file) + filename = req.get_selector() + localfile = url2pathname(filename) try: stats = os.stat(localfile) size = stats.st_size modified = email.utils.formatdate(stats.st_mtime, usegmt=True) - mtype = mimetypes.guess_type(file)[0] + mtype = mimetypes.guess_type(filename)[0] headers = mimetools.Message(StringIO( 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) @@ -1287,8 +1287,11 @@ class FileHandler(BaseHandler): 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://'+ host + file) + if host: + origurl = 'file://' + host + filename + else: + origurl = 'file://' + filename + return addinfourl(open(localfile, 'rb'), headers, origurl) except OSError, msg: # urllib2 users shouldn't expect OSErrors coming from urlopen() raise URLError(msg)