]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #12923: Reset FancyURLopener's redirect counter even on exception
authorMartin Panter <vadmium+py@gmail.com>
Thu, 4 Feb 2016 06:01:35 +0000 (06:01 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Thu, 4 Feb 2016 06:01:35 +0000 (06:01 +0000)
Based on patches by Brian Brazil and Daniel Rocco.

Lib/test/test_urllib.py
Lib/urllib.py
Misc/ACKS
Misc/NEWS

index adffb5731f046228bd45635480c9fbd4251559b2..e14cccca167d6e89df022e80eaada4fd19f840df 100644 (file)
@@ -209,10 +209,26 @@ Connection: close
 Content-Type: text/html; charset=iso-8859-1
 """)
         try:
-            self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
+            msg = "Redirection to url 'file:"
+            with self.assertRaisesRegexp(IOError, msg):
+                urllib.urlopen("http://python.org/")
         finally:
             self.unfakehttp()
 
+    def test_redirect_limit_independent(self):
+        # Ticket #12923: make sure independent requests each use their
+        # own retry limit.
+        for i in range(urllib.FancyURLopener().maxtries):
+            self.fakehttp(b'''HTTP/1.1 302 Found
+Location: file://guidocomputer.athome.com:/python/license
+Connection: close
+''')
+            try:
+                self.assertRaises(IOError, urllib.urlopen,
+                    "http://something")
+            finally:
+                self.unfakehttp()
+
     def test_empty_socket(self):
         # urlopen() raises IOError if the underlying socket does not send any
         # data. (#1680230)
index ccb05749355dfaa3c32cf1c9a25c76585cdd5580..0cb3df9b223f322bd593c1dfe8e0695889217544 100644 (file)
@@ -629,18 +629,20 @@ class FancyURLopener(URLopener):
     def http_error_302(self, url, fp, errcode, errmsg, headers, data=None):
         """Error 302 -- relocated (temporarily)."""
         self.tries += 1
-        if self.maxtries and self.tries >= self.maxtries:
-            if hasattr(self, "http_error_500"):
-                meth = self.http_error_500
-            else:
-                meth = self.http_error_default
+        try:
+            if self.maxtries and self.tries >= self.maxtries:
+                if hasattr(self, "http_error_500"):
+                    meth = self.http_error_500
+                else:
+                    meth = self.http_error_default
+                return meth(url, fp, 500,
+                            "Internal Server Error: Redirect Recursion",
+                            headers)
+            result = self.redirect_internal(url, fp, errcode, errmsg,
+                                            headers, data)
+            return result
+        finally:
             self.tries = 0
-            return meth(url, fp, 500,
-                        "Internal Server Error: Redirect Recursion", headers)
-        result = self.redirect_internal(url, fp, errcode, errmsg, headers,
-                                        data)
-        self.tries = 0
-        return result
 
     def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
         if 'location' in headers:
index df59e122aec99e957dff5bee9fbf23e37241efb2..2370960a15ffcf03fcf1e93aa9a1caf11bad5113 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1154,6 +1154,7 @@ Carl Robben
 Mark Roberts
 Andy Robinson
 Jim Robinson
+Daniel Rocco
 Mark Roddy
 Kevin Rodgers
 Sean Rodman
index 92f6c206eb624fb29f85c85420abaacde8d6e3a0..8d2c60231a41a55324d90ecf7a1d5d7fef7c1d8c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12923: Reset FancyURLopener's redirect counter even if there is an
+  exception.  Based on patches by Brian Brazil and Daniel Rocco.
+
 - Issue #25945: Fixed a crash when unpickle the functools.partial object with
   wrong state.  Fixed a leak in failed functools.partial constructor.
   "args" and "keywords" attributes of functools.partial have now always types