Skip to content

Commit 04da723

Browse files
jeremyhyltonzware
authored andcommitted
pass the original string error message from the ftplib error to URLError()
1 parent 0480052 commit 04da723

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Lib/test/test_urllib2.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import os
1010
import io
11+
import ftplib
1112
import socket
1213
import array
1314
import sys
@@ -754,7 +755,6 @@ def connect_ftp(self, user, passwd, host, port, dirs,
754755
self.ftpwrapper = MockFTPWrapper(self.data)
755756
return self.ftpwrapper
756757

757-
import ftplib
758758
data = "rheum rhaponicum"
759759
h = NullFTPHandler(data)
760760
h.parent = MockOpener()
@@ -794,6 +794,25 @@ def connect_ftp(self, user, passwd, host, port, dirs,
794794
self.assertEqual(headers.get("Content-type"), mimetype)
795795
self.assertEqual(int(headers["Content-length"]), len(data))
796796

797+
def test_ftp_error(self):
798+
class ErrorFTPHandler(urllib.request.FTPHandler):
799+
def __init__(self, exception):
800+
self._exception = exception
801+
802+
def connect_ftp(self, user, passwd, host, port, dirs,
803+
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
804+
raise self._exception
805+
806+
exception = ftplib.error_perm("500 OOPS: cannot change directory:/nonexistent")
807+
h = ErrorFTPHandler(exception)
808+
urlopen = urllib.request.build_opener(h).open
809+
try:
810+
urlopen("ftp://www.pythontest.net/")
811+
except urllib.error.URLError as raised:
812+
self.assertEqual(raised.reason, exception.args[0])
813+
else:
814+
self.fail("Did not raise ftplib exception")
815+
797816
def test_file(self):
798817
import email.utils
799818
h = urllib.request.FileHandler()

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ def ftp_open(self, req):
15551555
headers = email.message_from_string(headers)
15561556
return addinfourl(fp, headers, req.full_url)
15571557
except ftplib.all_errors as exp:
1558-
raise URLError(exp) from exp
1558+
raise URLError(exp.args[0]) from exp
15591559

15601560
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
15611561
return ftpwrapper(user, passwd, host, port, dirs, timeout,

0 commit comments

Comments
 (0)