|
| 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 |
| 10 | +from test.support.warnings_helper import check_no_resource_warning |
7 | 11 |
|
8 | 12 | import os |
9 | 13 | import socket |
@@ -143,6 +147,43 @@ def test_ftp(self): |
143 | 147 | ] |
144 | 148 | self._test_urls(urls, self._extra_handlers()) |
145 | 149 |
|
| 150 | + @support.requires_resource('walltime') |
| 151 | + @unittest.skipIf(sysconfig.get_platform() == 'linux-ppc64le', |
| 152 | + 'leaks on PPC64LE (gh-140691)') |
| 153 | + def test_ftp_no_leak(self): |
| 154 | + # gh-140691: When the data connection (but not control connection) |
| 155 | + # cannot be made established, we shouldn't leave an open socket object. |
| 156 | + |
| 157 | + class MockError(OSError): |
| 158 | + pass |
| 159 | + |
| 160 | + orig_create_connection = socket.create_connection |
| 161 | + def patched_create_connection(address, *args, **kwargs): |
| 162 | + """Simulate REJECTing connections to ports other than 21""" |
| 163 | + host, port = address |
| 164 | + if port != 21: |
| 165 | + raise MockError() |
| 166 | + return orig_create_connection(address, *args, **kwargs) |
| 167 | + |
| 168 | + url = 'ftp://www.pythontest.net/README' |
| 169 | + entry = url, None, urllib.error.URLError |
| 170 | + no_cache_handlers = [urllib.request.FTPHandler()] |
| 171 | + cache_handlers = self._extra_handlers() |
| 172 | + with mock.patch('socket.create_connection', patched_create_connection): |
| 173 | + with check_no_resource_warning(self): |
| 174 | + # Try without CacheFTPHandler |
| 175 | + self._test_urls([entry], handlers=no_cache_handlers, |
| 176 | + retry=False) |
| 177 | + with check_no_resource_warning(self): |
| 178 | + # Try with CacheFTPHandler (uncached) |
| 179 | + self._test_urls([entry], cache_handlers, retry=False) |
| 180 | + with check_no_resource_warning(self): |
| 181 | + # Try with CacheFTPHandler (cached) |
| 182 | + self._test_urls([entry], cache_handlers, retry=False) |
| 183 | + # Try without the mock: the handler should not use a closed connection |
| 184 | + with check_no_resource_warning(self): |
| 185 | + self._test_urls([url], cache_handlers, retry=False) |
| 186 | + |
146 | 187 | def test_file(self): |
147 | 188 | TESTFN = os_helper.TESTFN |
148 | 189 | f = open(TESTFN, 'w') |
@@ -234,18 +275,16 @@ def _test_urls(self, urls, handlers, retry=True): |
234 | 275 | else: |
235 | 276 | req = expected_err = None |
236 | 277 |
|
| 278 | + if expected_err: |
| 279 | + context = self.assertRaises(expected_err) |
| 280 | + else: |
| 281 | + context = contextlib.nullcontext() |
| 282 | + |
237 | 283 | with socket_helper.transient_internet(url): |
238 | | - try: |
| 284 | + f = None |
| 285 | + with context: |
239 | 286 | f = urlopen(url, req, support.INTERNET_TIMEOUT) |
240 | | - # urllib.error.URLError is a subclass of OSError |
241 | | - except OSError as err: |
242 | | - if expected_err: |
243 | | - msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
244 | | - (expected_err, url, req, type(err), err)) |
245 | | - self.assertIsInstance(err, expected_err, msg) |
246 | | - else: |
247 | | - raise |
248 | | - else: |
| 287 | + if f is not None: |
249 | 288 | try: |
250 | 289 | with time_out, \ |
251 | 290 | socket_peer_reset, \ |
|
0 commit comments