bpo-36918: Fix "Exception ignored in" in test_urllib (GH-13996) · python/cpython@eb976e4
@@ -56,7 +56,7 @@ def FancyURLopener():
5656return urllib.request.FancyURLopener()
5757585859-def fakehttp(fakedata):
59+def fakehttp(fakedata, mock_close=False):
6060class FakeSocket(io.BytesIO):
6161io_refs = 1
6262@@ -90,15 +90,24 @@ class FakeHTTPConnection(http.client.HTTPConnection):
9090def connect(self):
9191self.sock = FakeSocket(self.fakedata)
9292 type(self).fakesock = self.sock
93+94+if mock_close:
95+# bpo-36918: HTTPConnection destructor calls close() which calls
96+# flush(). Problem: flush() calls self.fp.flush() which raises
97+# "ValueError: I/O operation on closed file" which is logged as an
98+# "Exception ignored in". Override close() to silence this error.
99+def close(self):
100+pass
93101FakeHTTPConnection.fakedata = fakedata
9410295103return FakeHTTPConnection
961049710598106class FakeHTTPMixin(object):
99-def fakehttp(self, fakedata):
107+def fakehttp(self, fakedata, mock_close=False):
108+fake_http_class = fakehttp(fakedata, mock_close=mock_close)
100109self._connection_class = http.client.HTTPConnection
101-http.client.HTTPConnection = fakehttp(fakedata)
110+http.client.HTTPConnection = fake_http_class
102111103112def unfakehttp(self):
104113http.client.HTTPConnection = self._connection_class
@@ -400,7 +409,7 @@ def test_read_bogus(self):
400409Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
401410Connection: close
402411Content-Type: text/html; charset=iso-8859-1
403-''')
412+''', mock_close=True)
404413try:
405414self.assertRaises(OSError, urlopen, "http://python.org/")
406415finally:
@@ -414,7 +423,7 @@ def test_invalid_redirect(self):
414423Location: file://guidocomputer.athome.com:/python/license
415424Connection: close
416425Content-Type: text/html; charset=iso-8859-1
417-''')
426+''', mock_close=True)
418427try:
419428msg = "Redirection to url 'file:"
420429with self.assertRaisesRegex(urllib.error.HTTPError, msg):
@@ -429,7 +438,7 @@ def test_redirect_limit_independent(self):
429438self.fakehttp(b'''HTTP/1.1 302 Found
430439Location: file://guidocomputer.athome.com:/python/license
431440Connection: close
432-''')
441+''', mock_close=True)
433442try:
434443self.assertRaises(urllib.error.HTTPError, urlopen,
435444"http://something")