MCPcopy Create free account
hub / github.com/python/cpython / test_ftp_no_leak

Method test_ftp_no_leak

Lib/test/test_urllib2net.py:153–185  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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
187 def test_file(self):
188 TESTFN = os_helper.TESTFN

Callers

nothing calls this directly

Calls 3

_extra_handlersMethod · 0.95
_test_urlsMethod · 0.95

Tested by

no test coverage detected