Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc2f042

Browse files
committed
Issue #16250: Fix URLError invocation with proper args
2 parents 5f9459f + cad7b31 commit cc2f042

3 files changed

Lines changed: 51 additions & 16 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,39 @@ def test_empty_socket(self):
268268
finally:
269269
self.unfakehttp()
270270

271+
def test_missing_localfile(self):
272+
# Test for #10836
273+
# 3.3 - URLError is not captured, explicit IOError is raised.
274+
with self.assertRaises(IOError):
275+
urlopen('file://localhost/a/file/which/doesnot/exists.py')
276+
277+
def test_file_notexists(self):
278+
fd, tmp_file = tempfile.mkstemp()
279+
tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/')
280+
try:
281+
self.assertTrue(os.path.exists(tmp_file))
282+
with urlopen(tmp_fileurl) as fobj:
283+
self.assertTrue(fobj)
284+
finally:
285+
os.close(fd)
286+
os.unlink(tmp_file)
287+
self.assertFalse(os.path.exists(tmp_file))
288+
# 3.3 - IOError instead of URLError
289+
with self.assertRaises(IOError):
290+
urlopen(tmp_fileurl)
291+
292+
def test_ftp_nohost(self):
293+
test_ftp_url = 'ftp:///path'
294+
# 3.3 - IOError instead of URLError
295+
with self.assertRaises(IOError):
296+
urlopen(test_ftp_url)
297+
298+
def test_ftp_nonexisting(self):
299+
# 3.3 - IOError instead of URLError
300+
with self.assertRaises(IOError):
301+
urlopen('ftp://localhost/a/file/which/doesnot/exists.py')
302+
303+
271304
def test_userpass_inurl(self):
272305
self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")
273306
try:
@@ -300,7 +333,7 @@ def test_userpass_inurl_w_spaces(self):
300333

301334
def test_URLopener_deprecation(self):
302335
with support.check_warnings(('',DeprecationWarning)):
303-
warn = urllib.request.URLopener()
336+
urllib.request.URLopener()
304337

305338
class urlretrieve_FileTests(unittest.TestCase):
306339
"""Test urllib.urlretrieve() on local files"""

Lib/urllib/request.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,9 +1413,9 @@ def open_local_file(self, req):
14131413
else:
14141414
origurl = 'file://' + filename
14151415
return addinfourl(open(localfile, 'rb'), headers, origurl)
1416-
except OSError as msg:
1416+
except OSError as exp:
14171417
# users shouldn't expect OSErrors coming from urlopen()
1418-
raise URLError(msg)
1418+
raise URLError(exp)
14191419
raise URLError('file not on local host')
14201420

14211421
def _safe_gethostbyname(host):
@@ -1474,8 +1474,8 @@ def ftp_open(self, req):
14741474
headers += "Content-length: %d\n" % retrlen
14751475
headers = email.message_from_string(headers)
14761476
return addinfourl(fp, headers, req.full_url)
1477-
except ftplib.all_errors as msg:
1478-
exc = URLError('ftp error: %s' % msg)
1477+
except ftplib.all_errors as exp:
1478+
exc = URLError('ftp error: %r' % exp)
14791479
raise exc.with_traceback(sys.exc_info()[2])
14801480

14811481
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
@@ -1870,7 +1870,7 @@ def open_https(self, url, data=None):
18701870
def open_file(self, url):
18711871
"""Use local file or FTP depending on form of URL."""
18721872
if not isinstance(url, str):
1873-
raise URLError('file error', 'proxy support for file protocol currently not implemented')
1873+
raise URLError('file error: proxy support for file protocol currently not implemented')
18741874
if url[:2] == '//' and url[2:3] != '/' and url[2:12].lower() != 'localhost/':
18751875
raise ValueError("file:// scheme is supported only on localhost")
18761876
else:
@@ -1885,7 +1885,7 @@ def open_local_file(self, url):
18851885
try:
18861886
stats = os.stat(localname)
18871887
except OSError as e:
1888-
raise URLError(e.errno, e.strerror, e.filename)
1888+
raise URLError(e.strerror, e.filename)
18891889
size = stats.st_size
18901890
modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
18911891
mtype = mimetypes.guess_type(url)[0]
@@ -1899,22 +1899,22 @@ def open_local_file(self, url):
18991899
return addinfourl(open(localname, 'rb'), headers, urlfile)
19001900
host, port = splitport(host)
19011901
if (not port
1902-
and socket.gethostbyname(host) in (localhost() + thishost())):
1902+
and socket.gethostbyname(host) in ((localhost(),) + thishost())):
19031903
urlfile = file
19041904
if file[:1] == '/':
19051905
urlfile = 'file://' + file
19061906
elif file[:2] == './':
19071907
raise ValueError("local file url may start with / or file:. Unknown url of type: %s" % url)
19081908
return addinfourl(open(localname, 'rb'), headers, urlfile)
1909-
raise URLError('local file error', 'not on local host')
1909+
raise URLError('local file error: not on local host')
19101910

19111911
def open_ftp(self, url):
19121912
"""Use FTP protocol."""
19131913
if not isinstance(url, str):
1914-
raise URLError('ftp error', 'proxy support for ftp protocol currently not implemented')
1914+
raise URLError('ftp error: proxy support for ftp protocol currently not implemented')
19151915
import mimetypes
19161916
host, path = splithost(url)
1917-
if not host: raise URLError('ftp error', 'no host given')
1917+
if not host: raise URLError('ftp error: no host given')
19181918
host, port = splitport(host)
19191919
user, host = splituser(host)
19201920
if user: user, passwd = splitpasswd(user)
@@ -1963,13 +1963,13 @@ def open_ftp(self, url):
19631963
headers += "Content-Length: %d\n" % retrlen
19641964
headers = email.message_from_string(headers)
19651965
return addinfourl(fp, headers, "ftp:" + url)
1966-
except ftperrors() as msg:
1967-
raise URLError('ftp error', msg).with_traceback(sys.exc_info()[2])
1966+
except ftperrors() as exp:
1967+
raise URLError('ftp error %r' % exp).with_traceback(sys.exc_info()[2])
19681968

19691969
def open_data(self, url, data=None):
19701970
"""Use "data" URL."""
19711971
if not isinstance(url, str):
1972-
raise URLError('data error', 'proxy support for data protocol currently not implemented')
1972+
raise URLError('data error: proxy support for data protocol currently not implemented')
19731973
# ignore POSTed data
19741974
#
19751975
# syntax of data URLs:
@@ -2298,7 +2298,7 @@ def retrfile(self, file, type):
22982298
conn, retrlen = self.ftp.ntransfercmd(cmd)
22992299
except ftplib.error_perm as reason:
23002300
if str(reason)[:3] != '550':
2301-
raise URLError('ftp error', reason).with_traceback(
2301+
raise URLError('ftp error: %d' % reason).with_traceback(
23022302
sys.exc_info()[2])
23032303
if not conn:
23042304
# Set transfer mode to ASCII!
@@ -2310,7 +2310,7 @@ def retrfile(self, file, type):
23102310
try:
23112311
self.ftp.cwd(file)
23122312
except ftplib.error_perm as reason:
2313-
raise URLError('ftp error', reason) from reason
2313+
raise URLError('ftp error: %d' % reason) from reason
23142314
finally:
23152315
self.ftp.cwd(pwd)
23162316
cmd = 'LIST ' + file

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Core and Builtins
4949
Library
5050
-------
5151

52+
- Issue #16250: Fix URLError invocation with proper args.
53+
5254
- Issue #16116: Fix include and library paths to be correct when building C
5355
extensions in venvs.
5456

0 commit comments

Comments
 (0)