Skip to content

Commit 3ebef36

Browse files
committed
Issue #16250: Fix the invocations of URLError which had misplaced filename attribute for exception
1 parent 1348747 commit 3ebef36

3 files changed

Lines changed: 46 additions & 15 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,36 @@ def test_empty_socket(self):
270270

271271
def test_missing_localfile(self):
272272
# Test for #10836
273-
with self.assertRaises(urllib.error.URLError):
273+
with self.assertRaises(urllib.error.URLError) as e:
274274
urlopen('file://localhost/a/file/which/doesnot/exists.py')
275+
self.assertTrue(e.exception.filename)
276+
self.assertTrue(e.exception.reason)
277+
278+
def test_file_notexists(self):
279+
fd, tmp_file = tempfile.mkstemp()
280+
tmp_fileurl = 'file://' + tmp_file
281+
282+
self.assertTrue(os.path.exists(tmp_file))
283+
self.assertTrue(urlopen(tmp_fileurl))
284+
285+
os.unlink(tmp_file)
286+
self.assertFalse(os.path.exists(tmp_file))
287+
with self.assertRaises(urllib.error.URLError):
288+
urlopen(tmp_fileurl)
289+
290+
def test_ftp_nohost(self):
291+
test_ftp_url = 'ftp:///path'
292+
with self.assertRaises(urllib.error.URLError) as e:
293+
urlopen(test_ftp_url)
294+
self.assertFalse(e.exception.filename)
295+
self.assertTrue(e.exception.reason)
296+
297+
def test_ftp_nonexisting(self):
298+
with self.assertRaises(urllib.error.URLError) as e:
299+
urlopen('ftp://localhost/a/file/which/doesnot/exists.py')
300+
self.assertFalse(e.exception.filename)
301+
self.assertTrue(e.exception.reason)
302+
275303

276304
def test_userpass_inurl(self):
277305
self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")
@@ -305,7 +333,7 @@ def test_userpass_inurl_w_spaces(self):
305333

306334
def test_URLopener_deprecation(self):
307335
with support.check_warnings(('',DeprecationWarning)):
308-
warn = urllib.request.URLopener()
336+
urllib.request.URLopener()
309337

310338
class urlretrieve_FileTests(unittest.TestCase):
311339
"""Test urllib.urlretrieve() on local files"""

Lib/urllib/request.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,9 +1419,9 @@ def open_local_file(self, req):
14191419
else:
14201420
origurl = 'file://' + filename
14211421
return addinfourl(open(localfile, 'rb'), headers, origurl)
1422-
except OSError as msg:
1422+
except OSError as exp:
14231423
# users shouldn't expect OSErrors coming from urlopen()
1424-
raise URLError(msg)
1424+
raise URLError(exp)
14251425
raise URLError('file not on local host')
14261426

14271427
def _safe_gethostbyname(host):
@@ -1480,8 +1480,8 @@ def ftp_open(self, req):
14801480
headers += "Content-length: %d\n" % retrlen
14811481
headers = email.message_from_string(headers)
14821482
return addinfourl(fp, headers, req.full_url)
1483-
except ftplib.all_errors as msg:
1484-
exc = URLError('ftp error: %s' % msg)
1483+
except ftplib.all_errors as exp:
1484+
exc = URLError('ftp error: %r' % exp)
14851485
raise exc.with_traceback(sys.exc_info()[2])
14861486

14871487
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
@@ -1876,7 +1876,7 @@ def open_https(self, url, data=None):
18761876
def open_file(self, url):
18771877
"""Use local file or FTP depending on form of URL."""
18781878
if not isinstance(url, str):
1879-
raise URLError('file error', 'proxy support for file protocol currently not implemented')
1879+
raise URLError('file error: proxy support for file protocol currently not implemented')
18801880
if url[:2] == '//' and url[2:3] != '/' and url[2:12].lower() != 'localhost/':
18811881
raise ValueError("file:// scheme is supported only on localhost")
18821882
else:
@@ -1912,15 +1912,15 @@ def open_local_file(self, url):
19121912
elif file[:2] == './':
19131913
raise ValueError("local file url may start with / or file:. Unknown url of type: %s" % url)
19141914
return addinfourl(open(localname, 'rb'), headers, urlfile)
1915-
raise URLError('local file error', 'not on local host')
1915+
raise URLError('local file error: not on local host')
19161916

19171917
def open_ftp(self, url):
19181918
"""Use FTP protocol."""
19191919
if not isinstance(url, str):
1920-
raise URLError('ftp error', 'proxy support for ftp protocol currently not implemented')
1920+
raise URLError('ftp error: proxy support for ftp protocol currently not implemented')
19211921
import mimetypes
19221922
host, path = splithost(url)
1923-
if not host: raise URLError('ftp error', 'no host given')
1923+
if not host: raise URLError('ftp error: no host given')
19241924
host, port = splitport(host)
19251925
user, host = splituser(host)
19261926
if user: user, passwd = splitpasswd(user)
@@ -1969,13 +1969,13 @@ def open_ftp(self, url):
19691969
headers += "Content-Length: %d\n" % retrlen
19701970
headers = email.message_from_string(headers)
19711971
return addinfourl(fp, headers, "ftp:" + url)
1972-
except ftperrors() as msg:
1973-
raise URLError('ftp error', msg).with_traceback(sys.exc_info()[2])
1972+
except ftperrors() as exp:
1973+
raise URLError('ftp error %r' % exp).with_traceback(sys.exc_info()[2])
19741974

19751975
def open_data(self, url, data=None):
19761976
"""Use "data" URL."""
19771977
if not isinstance(url, str):
1978-
raise URLError('data error', 'proxy support for data protocol currently not implemented')
1978+
raise URLError('data error: proxy support for data protocol currently not implemented')
19791979
# ignore POSTed data
19801980
#
19811981
# syntax of data URLs:
@@ -2304,7 +2304,7 @@ def retrfile(self, file, type):
23042304
conn, retrlen = self.ftp.ntransfercmd(cmd)
23052305
except ftplib.error_perm as reason:
23062306
if str(reason)[:3] != '550':
2307-
raise URLError('ftp error', reason).with_traceback(
2307+
raise URLError('ftp error: %d' % reason).with_traceback(
23082308
sys.exc_info()[2])
23092309
if not conn:
23102310
# Set transfer mode to ASCII!
@@ -2316,7 +2316,7 @@ def retrfile(self, file, type):
23162316
try:
23172317
self.ftp.cwd(file)
23182318
except ftplib.error_perm as reason:
2319-
raise URLError('ftp error', reason) from reason
2319+
raise URLError('ftp error: %d' % reason) from reason
23202320
finally:
23212321
self.ftp.cwd(pwd)
23222322
cmd = 'LIST ' + file

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Core and Builtins
5959
Library
6060
-------
6161

62+
- Issue #16250: Fix the invocations of URLError which had misplaced filename
63+
attribute for exception.
64+
6265
- Issue #10836: Fix exception raised when file not found in urlretrieve
6366
Initial patch by Ezio Melotti.
6467

0 commit comments

Comments
 (0)