]> granicus.if.org Git - python/commitdiff
Issue #12133: fix a ResourceWarning in urllib.request
authorVictor Stinner <victor.stinner@haypocalc.com>
Fri, 17 Jun 2011 12:01:18 +0000 (14:01 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Fri, 17 Jun 2011 12:01:18 +0000 (14:01 +0200)
AbstractHTTPHandler.do_open() of urllib.request closes the HTTP connection if
its getresponse() method fails with a socket error. Patch written by Ezio
Melotti.

Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/NEWS

index e9fb2fc271f12715a8551c6677501a27a752ecf0..58ef83611deadce383cbdbdc3c61c553d0682987 100644 (file)
@@ -317,6 +317,9 @@ class MockHTTPClass:
     def getresponse(self):
         return MockHTTPResponse(MockFile(), {}, 200, "OK")
 
+    def close(self):
+        pass
+
 class MockHandler:
     # useful for testing handler machinery
     # see add_ordered_mock_handlers() docstring
index 5325d62c4098cb93b41e63b5ad6ca4f4aed7ee54..35fd1f136f0528d70d6b2377222aaab8d99f9453 100644 (file)
@@ -1137,6 +1137,8 @@ class AbstractHTTPHandler(BaseHandler):
             r = h.getresponse()  # an HTTPResponse instance
         except socket.error as err:
             raise URLError(err)
+        finally:
+            h.close()
 
         r.url = req.get_full_url()
         # This line replaces the .msg attribute of the HTTPResponse
index 75d10827ca43573fb4bb99a86d209d4001254fdf..6cc0f03637d8ca026c24f4b2fc55ce5efcd3ea1b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #12133: AbstractHTTPHandler.do_open() of urllib.request closes the HTTP
+  connection if its getresponse() method fails with a socket error. Patch
+  written by Ezio Melotti.
+
 - Issue #9284: Allow inspect.findsource() to find the source of doctest
   functions.