summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_urllib2_localnet.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-03-05 08:06:26 (GMT)
committerGitHub <noreply@github.com>2019-03-05 08:06:26 (GMT)
commit5b10b9824780b2181158902067912ee9e7b04657 (patch)
tree1c89bea944e6638eb008c8f106b2ee48cc9448d1 /Lib/test/test_urllib2_localnet.py
parent9e4861f52349011cd5916eef8e8344575e8ac426 (diff)
downloadcpython-5b10b9824780b2181158902067912ee9e7b04657.zip
cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.gz
cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.bz2
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 2). (GH-10929)
Diffstat (limited to 'Lib/test/test_urllib2_localnet.py')
-rw-r--r--Lib/test/test_urllib2_localnet.py28
1 files changed, 12 insertions, 16 deletions
diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
index 77dec0c..591b48d 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -371,10 +371,9 @@ class ProxyAuthTests(unittest.TestCase):
self.proxy_digest_handler.add_password(self.REALM, self.URL,
self.USER, self.PASSWD)
self.digest_auth_handler.set_qop("auth")
- result = self.opener.open(self.URL)
- while result.read():
- pass
- result.close()
+ with self.opener.open(self.URL) as result:
+ while result.read():
+ pass
def test_proxy_qop_auth_int_works_or_throws_urlerror(self):
self.proxy_digest_handler.add_password(self.REALM, self.URL,
@@ -386,11 +385,11 @@ class ProxyAuthTests(unittest.TestCase):
# It's okay if we don't support auth-int, but we certainly
# shouldn't receive any kind of exception here other than
# a URLError.
- result = None
- if result:
- while result.read():
- pass
- result.close()
+ pass
+ else:
+ with result:
+ while result.read():
+ pass
def GetRequestHandler(responses):
@@ -611,14 +610,11 @@ class TestUrlopen(unittest.TestCase):
def test_basic(self):
handler = self.start_server()
- open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
- for attr in ("read", "close", "info", "geturl"):
- self.assertTrue(hasattr(open_url, attr), "object returned from "
- "urlopen lacks the %s attribute" % attr)
- try:
+ with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
+ for attr in ("read", "close", "info", "geturl"):
+ self.assertTrue(hasattr(open_url, attr), "object returned from "
+ "urlopen lacks the %s attribute" % attr)
self.assertTrue(open_url.read(), "calling 'read' failed")
- finally:
- open_url.close()
def test_info(self):
handler = self.start_server()