diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-10-22 01:28:21 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-10-22 01:28:21 (GMT) |
commit | d60ea5ff009e7e48be78c388f642cd64287655f7 (patch) | |
tree | f8ef370f6c48d7547734ba5126d51c7adbf02eae /Lib/test/test_urllib2_localnet.py | |
parent | 3b557991d4a7626cf12baf2277bc87acbc439744 (diff) | |
download | cpython-d60ea5ff009e7e48be78c388f642cd64287655f7.zip cpython-d60ea5ff009e7e48be78c388f642cd64287655f7.tar.gz cpython-d60ea5ff009e7e48be78c388f642cd64287655f7.tar.bz2 |
Issue #26620: Fix ResourceWarning in test_urllib2_localnet
* Use context manager on urllib objects to ensure that they are closed on error
* Use self.addCleanup() to cleanup resources even if a test is interrupted
with CTRL+c
This backports a patch by Victor Stinner.
Diffstat (limited to 'Lib/test/test_urllib2_localnet.py')
-rw-r--r-- | Lib/test/test_urllib2_localnet.py | 39 |
1 files changed, 18 insertions, 21 deletions
diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py index c8b37ee..e9564fd 100644 --- a/Lib/test/test_urllib2_localnet.py +++ b/Lib/test/test_urllib2_localnet.py @@ -289,12 +289,12 @@ class BasicAuthTests(unittest.TestCase): def http_server_with_basic_auth_handler(*args, **kwargs): return BasicAuthHandler(*args, **kwargs) self.server = LoopbackHttpServerThread(http_server_with_basic_auth_handler) + self.addCleanup(self.server.stop) self.server_url = 'http://127.0.0.1:%s' % self.server.port self.server.start() self.server.ready.wait() def tearDown(self): - self.server.stop() super(BasicAuthTests, self).tearDown() def test_basic_auth_success(self): @@ -438,17 +438,13 @@ class TestUrlopen(unittest.TestCase): def setUp(self): super(TestUrlopen, self).setUp() + # Ignore proxies for localhost tests. - self.old_environ = os.environ.copy() + def restore_environ(old_environ): + os.environ.clear() + os.environ.update(old_environ) + self.addCleanup(restore_environ, os.environ.copy()) os.environ['NO_PROXY'] = '*' - self.server = None - - def tearDown(self): - if self.server is not None: - self.server.stop() - os.environ.clear() - os.environ.update(self.old_environ) - super(TestUrlopen, self).tearDown() def urlopen(self, url, data=None, **kwargs): l = [] @@ -469,6 +465,7 @@ class TestUrlopen(unittest.TestCase): handler = GetRequestHandler(responses) self.server = LoopbackHttpServerThread(handler) + self.addCleanup(self.server.stop) self.server.start() self.server.ready.wait() port = self.server.port @@ -592,7 +589,8 @@ class TestUrlopen(unittest.TestCase): handler = self.start_server() req = urllib.request.Request("http://localhost:%s/" % handler.port, headers={"Range": "bytes=20-39"}) - urllib.request.urlopen(req) + with urllib.request.urlopen(req): + pass self.assertEqual(handler.headers_received["Range"], "bytes=20-39") def test_basic(self): @@ -608,22 +606,21 @@ class TestUrlopen(unittest.TestCase): def test_info(self): handler = self.start_server() - try: - open_url = urllib.request.urlopen( - "http://localhost:%s" % handler.port) + open_url = urllib.request.urlopen( + "http://localhost:%s" % handler.port) + with open_url: info_obj = open_url.info() - self.assertIsInstance(info_obj, email.message.Message, - "object returned by 'info' is not an " - "instance of email.message.Message") - self.assertEqual(info_obj.get_content_subtype(), "plain") - finally: - self.server.stop() + self.assertIsInstance(info_obj, email.message.Message, + "object returned by 'info' is not an " + "instance of email.message.Message") + self.assertEqual(info_obj.get_content_subtype(), "plain") def test_geturl(self): # Make sure same URL as opened is returned by geturl. handler = self.start_server() open_url = urllib.request.urlopen("http://localhost:%s" % handler.port) - url = open_url.geturl() + with open_url: + url = open_url.geturl() self.assertEqual(url, "http://localhost:%s" % handler.port) def test_iteration(self): |