[2.7] bpo-27973 - Fix for urllib.urlretrieve() failing on second ftp … · python/cpython@f82e59a (original) (raw)

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
1 +import tempfile
1 2 import unittest
2 3 from test import test_support
4 +from test.test_urllib2net import skip_ftp_test_on_travis
3 5
4 6 import socket
5 7 import urllib
@@ -213,14 +215,49 @@ def test_context_argument(self):
213 215 self.assertIn("Python", response.read())
214 216
215 217
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 +
216 252 def test_main():
217 253 test_support.requires('network')
218 254 with test_support.check_py3k_warnings(
219 255 ("urllib.urlopen.. has been removed", DeprecationWarning)):
220 256 test_support.run_unittest(URLTimeoutTest,
221 257 urlopenNetworkTests,
222 258 urlretrieveNetworkTests,
223 -urlopen_HttpsTests)
259 +urlopen_HttpsTests,
260 +urlopen_FTPTest)
224 261
225 262 if __name__ == "__main__":
226 263 test_main()