# HG changeset patch # User Martin Panter # Date 1405579387 0 # Thu Jul 17 06:43:07 2014 +0000 # Branch 3.4 # Node ID b0bab299532438c088affa4c1543760456166173 # Parent c4c819e754773b5900e10e0b8e85681bacc31962 Close HTTPConnection on invalid response diff -r c4c819e75477 -r b0bab2995324 Lib/urllib/request.py --- a/Lib/urllib/request.py Thu Jul 17 06:42:59 2014 +0000 +++ b/Lib/urllib/request.py Thu Jul 17 06:43:07 2014 +0000 @@ -1186,18 +1186,21 @@ h.set_tunnel(req._tunnel_host, headers=tunnel_headers) try: - h.request(req.get_method(), req.selector, req.data, headers) - except OSError as err: # timeout error + try: + h.request(req.get_method(), req.selector, req.data, headers) + except OSError as err: # timeout error + raise URLError(err) + r = h.getresponse() + except: h.close() - 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 + raise + + # 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