| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| import contextlib | ||
| import errno | ||
| import sysconfig | ||
| import unittest | ||
| from unittest import mock | ||
| from test import support | ||
| from test.support import os_helper | ||
| from test.support import socket_helper | ||
| from test.support import ResourceDenied | ||
| from test.support.warnings_helper import check_no_resource_warning | ||
|
|
||
| import os | ||
| import socket | ||
| Expand Down Expand Up | @@ -143,6 +147,43 @@ def test_ftp(self): | |
| ] | ||
| self._test_urls(urls, self._extra_handlers()) | ||
|
|
||
| @support.requires_resource('walltime') | ||
| @unittest.skipIf(sysconfig.get_platform() == 'linux-ppc64le', | ||
| 'leaks on PPC64LE (gh-140691)') | ||
| def test_ftp_no_leak(self): | ||
| # gh-140691: When the data connection (but not control connection) | ||
| # cannot be made established, we shouldn't leave an open socket object. | ||
|
|
||
| class MockError(OSError): | ||
|
Comment thread
cmaloney marked this conversation as resolved.
|
||
| pass | ||
|
|
||
| orig_create_connection = socket.create_connection | ||
| def patched_create_connection(address, *args, **kwargs): | ||
| """Simulate REJECTing connections to ports other than 21""" | ||
| host, port = address | ||
| if port != 21: | ||
| raise MockError() | ||
| return orig_create_connection(address, *args, **kwargs) | ||
|
|
||
| url = 'ftp://www.pythontest.net/README' | ||
| entry = url, None, urllib.error.URLError | ||
| no_cache_handlers = [urllib.request.FTPHandler()] | ||
| cache_handlers = self._extra_handlers() | ||
| with mock.patch('socket.create_connection', patched_create_connection): | ||
| with check_no_resource_warning(self): | ||
| # Try without CacheFTPHandler | ||
| self._test_urls([entry], handlers=no_cache_handlers, | ||
| retry=False) | ||
| with check_no_resource_warning(self): | ||
| # Try with CacheFTPHandler (uncached) | ||
| self._test_urls([entry], cache_handlers, retry=False) | ||
| with check_no_resource_warning(self): | ||
| # Try with CacheFTPHandler (cached) | ||
| self._test_urls([entry], cache_handlers, retry=False) | ||
| # Try without the mock: the handler should not use a closed connection | ||
| with check_no_resource_warning(self): | ||
| self._test_urls([url], cache_handlers, retry=False) | ||
|
|
||
| def test_file(self): | ||
| TESTFN = os_helper.TESTFN | ||
| f = open(TESTFN, 'w') | ||
| Expand Down Expand Up | @@ -234,18 +275,16 @@ def _test_urls(self, urls, handlers, retry=True): | |
| else: | ||
| req = expected_err = None | ||
|
|
||
| if expected_err: | ||
| context = self.assertRaises(expected_err) | ||
| else: | ||
| context = contextlib.nullcontext() | ||
|
|
||
| with socket_helper.transient_internet(url): | ||
| try: | ||
| f = None | ||
| with context: | ||
| f = urlopen(url, req, support.INTERNET_TIMEOUT) | ||
| # urllib.error.URLError is a subclass of OSError | ||
| except OSError as err: | ||
| if expected_err: | ||
| msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % | ||
| (expected_err, url, req, type(err), err)) | ||
| self.assertIsInstance(err, expected_err, msg) | ||
| else: | ||
| raise | ||
|
Comment thread
Comment on lines
-242
to
-247
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality(The new context (assertRaises) handles the expected error; printing information is nowadays covered by subTest)
Sorry, something went wrong.
All reactions
|
||
| else: | ||
| if f is not None: | ||
| try: | ||
| with time_out, \ | ||
| socket_peer_reset, \ | ||
| Expand Down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| In :mod:`urllib.request`, when opening a FTP URL fails because a data | ||
| connection cannot be made, the control connection's socket is now closed to | ||
| avoid a :exc:`ResourceWarning`. |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.