[Python-checkins] cpython (merge 3.2 -> default): (Merge 3.2) Issue #12133: fix a ResourceWarning in urllib.request

victor.stinner python-checkins at python.org
Fri Jun 17 14:02:33 CEST 2011


http://hg.python.org/cpython/rev/57a98feb508e
changeset:   70836:57a98feb508e
parent:      70834:144cea8db9a5
parent:      70835:ad6bdfd7dd4b
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Fri Jun 17 14:02:18 2011 +0200
summary:
  (Merge 3.2) Issue #12133: fix a ResourceWarning in urllib.request

AbstractHTTPHandler.do_open() of urllib.request closes the HTTP connection if
its getresponse() method fails with a socket error. Patch written by Ezio
Melotti.

files:
  Lib/test/test_urllib2.py |  3 +++
  Lib/urllib/request.py    |  2 ++
  Misc/NEWS                |  4 ++++
  3 files changed, 9 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -318,6 +318,9 @@
     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
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1146,6 +1146,8 @@
             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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -193,6 +193,10 @@
 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 #12240: Allow multiple setup hooks in packaging's setup.cfg files.
   Original patch by Erik Bray.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list