Skip to content

Commit 6e25228

Browse files
miss-islingtonhembergerorsenthil
authored
[3.11] gh-81403: Fix for CacheFTPHandler in urllib (GH-13951) (#103705)
* gh-81403: Fix for CacheFTPHandler in urllib (GH-13951) bpo-37222: Fix for CacheFTPHandler in urllib A call to FTP.ntransfercmd must be followed by FTP.voidresp to clear the "end transfer" message. Without this, the client and server get out of sync, which will result in an error if the FTP instance is reused to open a second URL. This scenario occurs for even the most basic usage of CacheFTPHandler. Reverts the patch merged as a resolution to bpo-16270 and adds a test case for the CacheFTPHandler in test_urllib2net.py. (cherry picked from commit e38bebb) Co-authored-by: Dan Hemberger <[email protected]> Co-authored-by: Senthil Kumaran <[email protected]> * Added NEWS entry. --------- Co-authored-by: Dan Hemberger <[email protected]> Co-authored-by: Senthil Kumaran <[email protected]>
1 parent a43dbe1 commit 6e25228

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_urllib2net.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def setUp(self):
134134
# They do sometimes catch some major disasters, though.
135135

136136
def test_ftp(self):
137+
# Testing the same URL twice exercises the caching in CacheFTPHandler
137138
urls = [
139+
'ftp://www.pythontest.net/README',
138140
'ftp://www.pythontest.net/README',
139141
('ftp://www.pythontest.net/non-existent-file',
140142
None, urllib.error.URLError),

Lib/urllib/request.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,13 @@ def retrfile(self, file, type):
24692469
return (ftpobj, retrlen)
24702470

24712471
def endtransfer(self):
2472+
if not self.busy:
2473+
return
24722474
self.busy = 0
2475+
try:
2476+
self.ftp.voidresp()
2477+
except ftperrors():
2478+
pass
24732479

24742480
def close(self):
24752481
self.keepalive = False
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`urllib.request.CacheFTPHandler` no longer raises :class:`URLError`
2+
if a cached FTP instance is reused. ftplib's endtransfer method calls
3+
voidresp to drain the connection to handle FTP instance reuse properly.

0 commit comments

Comments
 (0)