summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-24 12:55:58 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-03-24 12:55:58 (GMT)
commitbc6b72ed06318a80a68b4bbf513a0e055b102e11 (patch)
tree0df641245428f706ff77cb9d5ce38834e74d0d89 /Lib
parente321274b2eeaf9cfaccbc8d40c4056ab72040e1b (diff)
downloadcpython-bc6b72ed06318a80a68b4bbf513a0e055b102e11.zip
cpython-bc6b72ed06318a80a68b4bbf513a0e055b102e11.tar.gz
cpython-bc6b72ed06318a80a68b4bbf513a0e055b102e11.tar.bz2
Closes #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
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_urllib2_localnet.py39
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):