[Python-checkins] cpython (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/ad6bdfd7dd4b
changeset:   70835:ad6bdfd7dd4b
branch:      3.2
parent:      70831:e6e7e42efdc2
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Fri Jun 17 14:01:18 2011 +0200
summary:
  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
@@ -317,6 +317,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
@@ -1137,6 +1137,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
@@ -25,6 +25,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 #9284: Allow inspect.findsource() to find the source of doctest
   functions.
 

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


More information about the Python-checkins mailing list