Skip to content

gh-122909: Pass ftp error strings to URLError constructor #122913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
import io
import ftplib
import socket
import array
import sys
Expand Down Expand Up @@ -754,7 +755,6 @@ def connect_ftp(self, user, passwd, host, port, dirs,
self.ftpwrapper = MockFTPWrapper(self.data)
return self.ftpwrapper

import ftplib
data = "rheum rhaponicum"
h = NullFTPHandler(data)
h.parent = MockOpener()
Expand Down Expand Up @@ -794,6 +794,27 @@ def connect_ftp(self, user, passwd, host, port, dirs,
self.assertEqual(headers.get("Content-type"), mimetype)
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):
import email.utils
h = urllib.request.FileHandler()
Expand Down
2 changes: 1 addition & 1 deletion Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def ftp_open(self, req):
headers = email.message_from_string(headers)
return addinfourl(fp, headers, req.full_url)
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):
return ftpwrapper(user, passwd, host, port, dirs, timeout,
Expand Down
Original file line number Diff line number Diff line change
@@ -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