Patch #1668100: urllib2 now correctly raises URLError instead of

OSError if accessing a local file via the file:// protocol fails.
This commit is contained in:
Georg Brandl 2007-03-13 08:14:27 +00:00
parent 8f032cbb05
commit ceede5c359
3 changed files with 25 additions and 18 deletions

View File

@ -626,11 +626,11 @@ class HandlerTests(unittest.TestCase):
for url in [ for url in [
"file://localhost:80%s" % urlpath, "file://localhost:80%s" % urlpath,
# XXXX bug: these fail with socket.gaierror, should be URLError "file:///file_does_not_exist.txt",
## "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
## os.getcwd(), TESTFN), os.getcwd(), TESTFN),
## "file://somerandomhost.ontheinternet.com%s/%s" % "file://somerandomhost.ontheinternet.com%s/%s" %
## (os.getcwd(), TESTFN), (os.getcwd(), TESTFN),
]: ]:
try: try:
f = open(TESTFN, "wb") f = open(TESTFN, "wb")

View File

@ -1214,19 +1214,23 @@ class FileHandler(BaseHandler):
host = req.get_host() host = req.get_host()
file = req.get_selector() file = req.get_selector()
localfile = url2pathname(file) localfile = url2pathname(file)
stats = os.stat(localfile) try:
size = stats.st_size stats = os.stat(localfile)
modified = email.utils.formatdate(stats.st_mtime, usegmt=True) size = stats.st_size
mtype = mimetypes.guess_type(file)[0] modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
headers = mimetools.Message(StringIO( mtype = mimetypes.guess_type(file)[0]
'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % headers = mimetools.Message(StringIO(
(mtype or 'text/plain', size, modified))) 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
if host: (mtype or 'text/plain', size, modified)))
host, port = splitport(host) if host:
if not host or \ host, port = splitport(host)
(not port and socket.gethostbyname(host) in self.get_names()): if not host or \
return addinfourl(open(localfile, 'rb'), (not port and socket.gethostbyname(host) in self.get_names()):
headers, 'file:'+file) 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') raise URLError('file not on local host')
class FTPHandler(BaseHandler): class FTPHandler(BaseHandler):

View File

@ -168,6 +168,9 @@ Core and builtins
Library 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 #1677862: Require a space or tab after import in .pth files.
- Patch #1192590: Fix pdb's "ignore" and "condition" commands so they trap - Patch #1192590: Fix pdb's "ignore" and "condition" commands so they trap