[Python-checkins] cpython (3.5): Close HTTP connections and responses in tests to avoid ResourceWarnings

martin.panter python-checkins at python.org
Fri Aug 12 08:18:33 EDT 2016


https://hg.python.org/cpython/rev/6a335f59c2ba
changeset:   102614:6a335f59c2ba
branch:      3.5
parent:      102609:dc31fa499359
user:        Martin Panter <vadmium+py at gmail.com>
date:        Fri Aug 12 11:59:52 2016 +0000
summary:
  Close HTTP connections and responses in tests to avoid ResourceWarnings

files:
  Lib/test/test_httplib.py |  12 +++++++++++-
  1 files changed, 11 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -1402,6 +1402,7 @@
             resp = h.getresponse()
             h.close()
             self.assertIn('nginx', resp.getheader('server'))
+            resp.close()
 
     @support.system_must_validate_cert
     def test_networked_trusted_by_default_cert(self):
@@ -1412,6 +1413,7 @@
             h.request('GET', '/')
             resp = h.getresponse()
             content_type = resp.getheader('content-type')
+            resp.close()
             h.close()
             self.assertIn('text/html', content_type)
 
@@ -1427,6 +1429,7 @@
             h.request('GET', '/')
             resp = h.getresponse()
             server_string = resp.getheader('server')
+            resp.close()
             h.close()
             self.assertIn('nginx', server_string)
 
@@ -1460,8 +1463,10 @@
         context.verify_mode = ssl.CERT_REQUIRED
         context.load_verify_locations(CERT_localhost)
         h = client.HTTPSConnection('localhost', server.port, context=context)
+        self.addCleanup(h.close)
         h.request('GET', '/nonexistent')
         resp = h.getresponse()
+        self.addCleanup(resp.close)
         self.assertEqual(resp.status, 404)
 
     def test_local_bad_hostname(self):
@@ -1486,13 +1491,18 @@
                                    check_hostname=False)
         h.request('GET', '/nonexistent')
         resp = h.getresponse()
+        resp.close()
+        h.close()
         self.assertEqual(resp.status, 404)
         # The context's check_hostname setting is used if one isn't passed to
         # HTTPSConnection.
         context.check_hostname = False
         h = client.HTTPSConnection('localhost', server.port, context=context)
         h.request('GET', '/nonexistent')
-        self.assertEqual(h.getresponse().status, 404)
+        resp = h.getresponse()
+        self.assertEqual(resp.status, 404)
+        resp.close()
+        h.close()
         # Passing check_hostname to HTTPSConnection should override the
         # context's setting.
         h = client.HTTPSConnection('localhost', server.port, context=context,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list