|
| 1 | +import contextlib |
1 | 2 | import errno |
| 3 | +import sysconfig |
2 | 4 | import unittest |
| 5 | +from unittest import mock |
3 | 6 | from test import support |
4 | 7 | from test.support import os_helper |
5 | 8 | from test.support import socket_helper |
6 | 9 | from test.support import ResourceDenied |
7 | 10 | from test.test_urllib2 import sanepathname2url |
| 11 | +from test.support.warnings_helper import check_no_resource_warning |
8 | 12 |
|
9 | 13 | import os |
10 | 14 | import socket |
@@ -144,6 +148,43 @@ def test_ftp(self): |
144 | 148 | ] |
145 | 149 | self._test_urls(urls, self._extra_handlers()) |
146 | 150 |
|
| 151 | + @support.requires_resource('walltime') |
| 152 | + @unittest.skipIf(sysconfig.get_platform() == 'linux-ppc64le', |
| 153 | + 'leaks on PPC64LE (gh-140691)') |
| 154 | + def test_ftp_no_leak(self): |
| 155 | + # gh-140691: When the data connection (but not control connection) |
| 156 | + # cannot be made established, we shouldn't leave an open socket object. |
| 157 | + |
| 158 | + class MockError(OSError): |
| 159 | + pass |
| 160 | + |
| 161 | + orig_create_connection = socket.create_connection |
| 162 | + def patched_create_connection(address, *args, **kwargs): |
| 163 | + """Simulate REJECTing connections to ports other than 21""" |
| 164 | + host, port = address |
| 165 | + if port != 21: |
| 166 | + raise MockError() |
| 167 | + return orig_create_connection(address, *args, **kwargs) |
| 168 | + |
| 169 | + url = 'ftp://www.pythontest.net/README' |
| 170 | + entry = url, None, urllib.error.URLError |
| 171 | + no_cache_handlers = [urllib.request.FTPHandler()] |
| 172 | + cache_handlers = self._extra_handlers() |
| 173 | + with mock.patch('socket.create_connection', patched_create_connection): |
| 174 | + with check_no_resource_warning(self): |
| 175 | + # Try without CacheFTPHandler |
| 176 | + self._test_urls([entry], handlers=no_cache_handlers, |
| 177 | + retry=False) |
| 178 | + with check_no_resource_warning(self): |
| 179 | + # Try with CacheFTPHandler (uncached) |
| 180 | + self._test_urls([entry], cache_handlers, retry=False) |
| 181 | + with check_no_resource_warning(self): |
| 182 | + # Try with CacheFTPHandler (cached) |
| 183 | + self._test_urls([entry], cache_handlers, retry=False) |
| 184 | + # Try without the mock: the handler should not use a closed connection |
| 185 | + with check_no_resource_warning(self): |
| 186 | + self._test_urls([url], cache_handlers, retry=False) |
| 187 | + |
147 | 188 | def test_file(self): |
148 | 189 | TESTFN = os_helper.TESTFN |
149 | 190 | f = open(TESTFN, 'w') |
@@ -256,18 +297,16 @@ def _test_urls(self, urls, handlers, retry=True): |
256 | 297 | else: |
257 | 298 | req = expected_err = None |
258 | 299 |
|
| 300 | + if expected_err: |
| 301 | + context = self.assertRaises(expected_err) |
| 302 | + else: |
| 303 | + context = contextlib.nullcontext() |
| 304 | + |
259 | 305 | with socket_helper.transient_internet(url): |
260 | | - try: |
| 306 | + f = None |
| 307 | + with context: |
261 | 308 | f = urlopen(url, req, support.INTERNET_TIMEOUT) |
262 | | - # urllib.error.URLError is a subclass of OSError |
263 | | - except OSError as err: |
264 | | - if expected_err: |
265 | | - msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
266 | | - (expected_err, url, req, type(err), err)) |
267 | | - self.assertIsInstance(err, expected_err, msg) |
268 | | - else: |
269 | | - raise |
270 | | - else: |
| 309 | + if f is not None: |
271 | 310 | try: |
272 | 311 | with time_out, \ |
273 | 312 | socket_peer_reset, \ |
|
0 commit comments