mirror of
https://github.com/python/cpython.git
synced 2025-10-23 07:02:24 +00:00
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.
This commit is contained in:
parent
0480052ea1
commit
77133f570d
3 changed files with 26 additions and 2 deletions
|
@ -8,6 +8,7 @@ from unittest import mock
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
|
import ftplib
|
||||||
import socket
|
import socket
|
||||||
import array
|
import array
|
||||||
import sys
|
import sys
|
||||||
|
@ -754,7 +755,6 @@ class HandlerTests(unittest.TestCase):
|
||||||
self.ftpwrapper = MockFTPWrapper(self.data)
|
self.ftpwrapper = MockFTPWrapper(self.data)
|
||||||
return self.ftpwrapper
|
return self.ftpwrapper
|
||||||
|
|
||||||
import ftplib
|
|
||||||
data = "rheum rhaponicum"
|
data = "rheum rhaponicum"
|
||||||
h = NullFTPHandler(data)
|
h = NullFTPHandler(data)
|
||||||
h.parent = MockOpener()
|
h.parent = MockOpener()
|
||||||
|
@ -794,6 +794,27 @@ class HandlerTests(unittest.TestCase):
|
||||||
self.assertEqual(headers.get("Content-type"), mimetype)
|
self.assertEqual(headers.get("Content-type"), mimetype)
|
||||||
self.assertEqual(int(headers["Content-length"]), len(data))
|
self.assertEqual(int(headers["Content-length"]), len(data))
|
||||||
|
|
||||||
|
def test_ftp_error(self):
|
||||||
|
class ErrorFTPHandler(urllib.request.FTPHandler):
|
||||||
|
def __init__(self, exception):
|
||||||
|
self._exception = exception
|
||||||
|
|
||||||
|
def connect_ftp(self, user, passwd, host, port, dirs,
|
||||||
|
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
|
||||||
|
raise self._exception
|
||||||
|
|
||||||
|
exception = ftplib.error_perm(
|
||||||
|
"500 OOPS: cannot change directory:/nonexistent")
|
||||||
|
h = ErrorFTPHandler(exception)
|
||||||
|
urlopen = urllib.request.build_opener(h).open
|
||||||
|
try:
|
||||||
|
urlopen("ftp://www.pythontest.net/")
|
||||||
|
except urllib.error.URLError as raised:
|
||||||
|
self.assertEqual(raised.reason,
|
||||||
|
f"ftp error: {exception.args[0]}")
|
||||||
|
else:
|
||||||
|
self.fail("Did not raise ftplib exception")
|
||||||
|
|
||||||
def test_file(self):
|
def test_file(self):
|
||||||
import email.utils
|
import email.utils
|
||||||
h = urllib.request.FileHandler()
|
h = urllib.request.FileHandler()
|
||||||
|
|
|
@ -1555,7 +1555,7 @@ class FTPHandler(BaseHandler):
|
||||||
headers = email.message_from_string(headers)
|
headers = email.message_from_string(headers)
|
||||||
return addinfourl(fp, headers, req.full_url)
|
return addinfourl(fp, headers, req.full_url)
|
||||||
except ftplib.all_errors as exp:
|
except ftplib.all_errors as exp:
|
||||||
raise URLError(exp) from exp
|
raise URLError(f"ftp error: {exp}") from exp
|
||||||
|
|
||||||
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
|
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
|
||||||
return ftpwrapper(user, passwd, host, port, dirs, timeout,
|
return ftpwrapper(user, passwd, host, port, dirs, timeout,
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
In urllib.request when URLError is raised opening an ftp URL, the exception
|
||||||
|
argument is now consistently a string. Earlier versions passed either a
|
||||||
|
string or an ftplib exception instance as the argument to URLError.
|
Loading…
Add table
Add a link
Reference in a new issue