8000 [2.7] bpo-27973 - Fix for urllib.urlretrieve() failing on second ftp … · stackless-dev/stackless@f82e59a · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit f82e59a

Browse files
authored
[2.7] bpo-27973 - Fix for urllib.urlretrieve() failing on second ftp transfer (python#1040)
* bpo-27973: Fix urllib.urlretrieve failing on subsequent ftp transfers from the same host. * bpo-35411: Skip test_urllibnet FTP tests on Travis CI.
1 parent 362ede2 commit f82e59a

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Lib/test/test_urllibnet.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import tempfile
12
import unittest
23
from test import test_support
4+
from test.test_urllib2net import skip_ftp_test_on_travis
35

46
import socket
57
import urllib
@@ -213,14 +215,49 @@ def test_context_argument(self):
213215
self.assertIn("Python", response.read())
214216

215217

218+
class urlopen_FTPTest(unittest.TestCase):
219+
FTP_TEST_FILE = 'ftp://www.pythontest.net/README'
220+
NUM_FTP_RETRIEVES = 3
221+
222+
@skip_ftp_test_on_travis
223+
def test_multiple_ftp_retrieves(self):
224+
225+
with test_support.transient_internet(self.FTP_TEST_FILE):
226+
try:
227+
for _ in range(self.NUM_FTP_RETRIEVES):
228+
with tempfile.NamedTemporaryFile() as fp:
229+
urllib.FancyURLopener().retrieve(self.FTP_TEST_FILE, fp.name)
230+
except IOError as e:
231+
self.fail("Failed FTP retrieve while accessing ftp url "
232+
"multiple times.\n Error message was : %s" % e)
233+
234+
@skip_ftp_test_on_travis
235+
def test_multiple_ftp_urlopen_same_host(self):
236+
with test_support.transient_internet(self.FTP_TEST_FILE):
237+
ftp_fds_to_close = []
238+
try:
239+
for _ in range(self.NUM_FTP_RETRIEVES):
240+
fd = urllib.urlopen(self.FTP_TEST_FILE)
241+
# test ftp open without closing fd as a supported scenario.
242+
ftp_fds_to_close.append(fd)
243+
except IOError as e:
244+
self.fail("Failed FTP binary file open. "
245+
"Error message was: %s" % e)
246+
finally:
247+
# close the open fds
248+
for fd in ftp_fds_to_close:
249+
fd.close()
250+
251+
216252
def test_main():
217253
test_support.requires('network')
218254
with test_support.check_py3k_warnings(
219255
("urllib.urlopen.. has been removed", DeprecationWarning)):
220256
test_support.run_unittest(URLTimeoutTest,
221257
urlopenNetworkTests,
222258
urlretrieveNetworkTests,
223-
urlopen_HttpsTests)
259+
urlopen_HttpsTests,
260+
urlopen_FTPTest)
224261

225262
if __name__ == "__main__":
226263
test_main()

Lib/urllib.py

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

936936
def endtransfer(self):
937+
if not self.busy:
938+
return
937939
self.busy = 0
940+
try:
941+
self.ftp.voidresp()
942+
except ftperrors():
943+
pass
938944

939945
def close(self):
940946
self.keepalive = False
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix urllib.urlretrieve failing on subsequent ftp transfers from the same
2+
host.

0 commit comments

Comments
 (0)
0