Skip to content

[2.7] bpo-27973 - Fix for urllib.urlretrieve() failing on second ftp transfer #1040

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
Dec 31, 2019
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
39 changes: 38 additions & 1 deletion Lib/test/test_urllibnet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import tempfile
import unittest
from test import test_support
from test.test_urllib2net import skip_ftp_test_on_travis

import socket
import urllib
Expand Down Expand Up @@ -213,14 +215,49 @@ def test_context_argument(self):
self.assertIn("Python", response.read())


class urlopen_FTPTest(unittest.TestCase):
FTP_TEST_FILE = 'ftp://www.pythontest.net/README'
NUM_FTP_RETRIEVES = 3

@skip_ftp_test_on_travis
def test_multiple_ftp_retrieves(self):

with test_support.transient_internet(self.FTP_TEST_FILE):
try:
for _ in range(self.NUM_FTP_RETRIEVES):
with tempfile.NamedTemporaryFile() as fp:
urllib.FancyURLopener().retrieve(self.FTP_TEST_FILE, fp.name)
except IOError as e:
self.fail("Failed FTP retrieve while accessing ftp url "
"multiple times.\n Error message was : %s" % e)

@skip_ftp_test_on_travis
def test_multiple_ftp_urlopen_same_host(self):
with test_support.transient_internet(self.FTP_TEST_FILE):
ftp_fds_to_close = []
try:
for _ in range(self.NUM_FTP_RETRIEVES):
fd = urllib.urlopen(self.FTP_TEST_FILE)
# test ftp open without closing fd as a supported scenario.
ftp_fds_to_close.append(fd)
except IOError as e:
self.fail("Failed FTP binary file open. "
"Error message was: %s" % e)
finally:
# close the open fds
for fd in ftp_fds_to_close:
fd.close()


def test_main():
test_support.requires('network')
with test_support.check_py3k_warnings(
("urllib.urlopen.. has been removed", DeprecationWarning)):
test_support.run_unittest(URLTimeoutTest,
urlopenNetworkTests,
urlretrieveNetworkTests,
urlopen_HttpsTests)
urlopen_HttpsTests,
urlopen_FTPTest)

if __name__ == "__main__":
test_main()
6 changes: 6 additions & 0 deletions Lib/urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,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,2 @@
Fix urllib.urlretrieve failing on subsequent ftp transfers from the same
host.