Skip to content

Commit 77133f5

Browse files
authored
gh-122909: Pass ftp error strings to URLError constructor (#122913)
* pass the original string error message from the ftplib error to URLError() * Update request.py Change error string for ftp error to be consistent with other errors reported for ftp * Add NEWS entry for change to urllib.request for ftp errors. * Track the change in the ftp error message in the test.
1 parent 0480052 commit 77133f5

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Lib/test/test_urllib2.py

Lines changed: 22 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,27 @@ 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(
807+
"500 OOPS: cannot change directory:/nonexistent")
808+
h = ErrorFTPHandler(exception)
809+
urlopen = urllib.request.build_opener(h).open
810+
try:
811+
urlopen("ftp://www.pythontest.net/")
812+
except urllib.error.URLError as raised:
813+
self.assertEqual(raised.reason,
814+
f"ftp error: {exception.args[0]}")
815+
else:
816+
self.fail("Did not raise ftplib exception")
817+
797818
def test_file(self):
798819
import email.utils
799820
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(f"ftp error: {exp}") from exp
15591559

15601560
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
15611561
return ftpwrapper(user, passwd, host, port, dirs, timeout,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In urllib.request when URLError is raised opening an ftp URL, the exception
2+
argument is now consistently a string. Earlier versions passed either a
3+
string or an ftplib exception instance as the argument to URLError.

0 commit comments

Comments
 (0)