diff options
| -rw-r--r-- | Lib/test/test_urllib2net.py | 2 | ||||
| -rw-r--r-- | Lib/urllib/request.py | 6 | ||||
| -rw-r--r-- | Misc/NEWS.d/next/Library/2023-04-22-22-14-09.gh-issue-81403.zVz9Td.rst | 3 |
3 files changed, 11 insertions, 0 deletions
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index 5da41c3..d8d882b 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -134,8 +134,10 @@ class OtherNetworkTests(unittest.TestCase): # 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), ] diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 73ad012..24911bb 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2469,7 +2469,13 @@ class ftpwrapper: 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 diff --git a/Misc/NEWS.d/next/Library/2023-04-22-22-14-09.gh-issue-81403.zVz9Td.rst b/Misc/NEWS.d/next/Library/2023-04-22-22-14-09.gh-issue-81403.zVz9Td.rst new file mode 100644 index 0000000..6adb71f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-22-22-14-09.gh-issue-81403.zVz9Td.rst @@ -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. |
