From: Senthil Kumaran Date: Sun, 29 Dec 2013 01:36:18 +0000 (-0800) Subject: Backporing the fix from Issue #12692 X-Git-Tag: v3.4.0b2~73^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b6fac245b5aaaf0b465c2f7f346c7f0ad82143f2;p=thirdparty%2FPython%2Fcpython.git Backporing the fix from Issue #12692 --- diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 33f90f48ca6e..d4c894534a80 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -283,6 +283,7 @@ class MockHTTPClass: self.req_headers = [] self.data = None self.raise_on_endheaders = False + self.sock = None self._tunnel_headers = {} def __call__(self, host, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index a7445d185d04..ef62acc71028 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1251,6 +1251,12 @@ class AbstractHTTPHandler(BaseHandler): raise URLError(err) else: r = h.getresponse() + # If the server does not send us a 'Connection: close' header, + # HTTPConnection assumes the socket should be left open. Manually + # mark the socket to be closed when this response object goes away. + if h.sock: + h.sock.close() + h.sock = None r.url = req.get_full_url() # This line replaces the .msg attribute of the HTTPResponse diff --git a/Misc/NEWS b/Misc/NEWS index b7924cb4bb1c..b6015af96639 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -29,6 +29,9 @@ Core and Builtins Library ------- +- Issue #12692: Backport the fix for ResourceWarning in test_urllib2net. This + also helps in closing the socket when Connection Close header is not sent. + - Issue #19422: Explicitly disallow non-SOCK_STREAM sockets in the ssl module, rather than silently let them emit clear text data.