Skip to content

[3.11] gh-81403: Fix for CacheFTPHandler in urllib (GH-13951) #103705

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 2 commits into from
Apr 23, 2023
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
2 changes: 2 additions & 0 deletions Lib/test/test_urllib2net.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def setUp(self):
# They do sometimes catch some major disasters, though.

def test_ftp(self):
# Testing the same URL twice exercises the caching in CacheFTPHandler
urls = [
'ftp://www.pythontest.net/README',
'ftp://www.pythontest.net/README',
('ftp://www.pythontest.net/non-existent-file',
None, urllib.error.URLError),
Expand Down
6 changes: 6 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,13 @@ def retrfile(self, file, type):
return (ftpobj, retrlen)

def endtransfer(self):
if not self.busy:
return
self.busy = 0
try:
self.ftp.voidresp()
except ftperrors():
pass

def close(self):
self.keepalive = False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:class:`urllib.request.CacheFTPHandler` no longer raises :class:`URLError`
if a cached FTP instance is reused. ftplib's endtransfer method calls
voidresp to drain the connection to handle FTP instance reuse properly.