[Python-checkins] cpython (merge 3.2 -> default): merge from 3.2 - Fix closes Issue12576 - fix urlopen behavior on sites which do

senthil.kumaran python-checkins at python.org
Wed Jul 27 02:25:57 CEST 2011


http://hg.python.org/cpython/rev/a45c8ce67c7d
changeset:   71517:a45c8ce67c7d
parent:      71515:683a9aeb5d58
parent:      71516:9eac48fbe21d
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Wed Jul 27 08:06:44 2011 +0800
summary:
  merge from 3.2 - Fix closes Issue12576 - fix urlopen behavior on sites which do not send (or obsfuscates) Connection: Close header.

files:
  Lib/test/test_urllib2net.py |  16 ++++++++++++++++
  Lib/urllib/request.py       |   9 ++++++---
  Misc/NEWS                   |   3 +++
  3 files changed, 25 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -174,6 +174,22 @@
             opener.open(request)
             self.assertEqual(request.get_header('User-agent'),'Test-Agent')
 
+    def test_sites_no_connection_close(self):
+        # Some sites do not send Connection: close header.
+        # Verify that those work properly. (#issue12576)
+
+        try:
+            with urllib.request.urlopen('http://www.imdb.com') as res:
+                pass
+        except ValueError as e:
+            self.fail("urlopen failed for sites not sending Connection:close")
+        else:
+            self.assertTrue(res)
+
+        req = urllib.request.urlopen('http://www.imdb.com')
+        res = req.read()
+        self.assertTrue(res)
+
     def _test_urls(self, urls, handlers, retry=True):
         import time
         import logging
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1143,11 +1143,14 @@
 
         try:
             h.request(req.get_method(), req.selector, req.data, headers)
-            r = h.getresponse()  # an HTTPResponse instance
-        except socket.error as err:
+        except socket.error as err: # timeout error
             raise URLError(err)
         finally:
-            h.close()
+            try:
+                r = h.getresponse() # an HTTPResponse instance
+            except Exception as exp:
+                h.close()
+                raise exp
 
         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
@@ -237,6 +237,9 @@
 Library
 -------
 
+- Issue #12576: Fix urlopen behavior on sites which do not send (or obfuscates)
+  Connection:close header.
+
 - Issue #12102: Document that buffered files must be flushed before being used
   with mmap. Patch by Steffen Daode Nurpmeso.
 

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


More information about the Python-checkins mailing list