[Python-checkins] bpo-36918: Fix "Exception ignored in" in test_urllib (GH-13996)

Miss Islington (bot) webhook-mailer at python.org
Tue Jun 11 22:26:08 EDT 2019


https://github.com/python/cpython/commit/9d37ae0bee25692572c201378cd0692df22fa2ac
commit: 9d37ae0bee25692572c201378cd0692df22fa2ac
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-06-11T19:26:02-07:00
summary:

bpo-36918: Fix "Exception ignored in" in test_urllib (GH-13996)


Mock the HTTPConnection.close() method in a few unit tests to avoid
logging "Exception ignored in: ..." messages.
(cherry picked from commit eb976e47e261760330c1bed224019b073b05e994)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
M Lib/test/test_urllib.py

diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index f9b2799d25bf..801f0fd647f4 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -56,7 +56,7 @@ def FancyURLopener():
         return urllib.request.FancyURLopener()
 
 
-def fakehttp(fakedata):
+def fakehttp(fakedata, mock_close=False):
     class FakeSocket(io.BytesIO):
         io_refs = 1
 
@@ -90,15 +90,24 @@ class FakeHTTPConnection(http.client.HTTPConnection):
         def connect(self):
             self.sock = FakeSocket(self.fakedata)
             type(self).fakesock = self.sock
+
+        if mock_close:
+            # bpo-36918: HTTPConnection destructor calls close() which calls
+            # flush(). Problem: flush() calls self.fp.flush() which raises
+            # "ValueError: I/O operation on closed file" which is logged as an
+            # "Exception ignored in". Override close() to silence this error.
+            def close(self):
+                pass
     FakeHTTPConnection.fakedata = fakedata
 
     return FakeHTTPConnection
 
 
 class FakeHTTPMixin(object):
-    def fakehttp(self, fakedata):
+    def fakehttp(self, fakedata, mock_close=False):
+        fake_http_class = fakehttp(fakedata, mock_close=mock_close)
         self._connection_class = http.client.HTTPConnection
-        http.client.HTTPConnection = fakehttp(fakedata)
+        http.client.HTTPConnection = fake_http_class
 
     def unfakehttp(self):
         http.client.HTTPConnection = self._connection_class
@@ -400,7 +409,7 @@ def test_read_bogus(self):
 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
 Connection: close
 Content-Type: text/html; charset=iso-8859-1
-''')
+''', mock_close=True)
         try:
             self.assertRaises(OSError, urlopen, "http://python.org/")
         finally:
@@ -414,7 +423,7 @@ def test_invalid_redirect(self):
 Location: file://guidocomputer.athome.com:/python/license
 Connection: close
 Content-Type: text/html; charset=iso-8859-1
-''')
+''', mock_close=True)
         try:
             msg = "Redirection to url 'file:"
             with self.assertRaisesRegex(urllib.error.HTTPError, msg):
@@ -429,7 +438,7 @@ def test_redirect_limit_independent(self):
             self.fakehttp(b'''HTTP/1.1 302 Found
 Location: file://guidocomputer.athome.com:/python/license
 Connection: close
-''')
+''', mock_close=True)
             try:
                 self.assertRaises(urllib.error.HTTPError, urlopen,
                     "http://something")



More information about the Python-checkins mailing list