changeset: 100162:d3be5c4507b4 parent: 100159:d00a1c46fff7 parent: 100160:eb69070e5382 user: Martin Panter date: Sat Feb 06 01:08:40 2016 +0000 files: Lib/urllib/request.py Misc/ACKS Misc/NEWS description: Issue #12923: Merge FancyURLopener fix from 3.5 diff -r d00a1c46fff7 -r d3be5c4507b4 Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Thu Feb 04 14:08:23 2016 -0500 +++ b/Lib/test/test_urllib.py Sat Feb 06 01:08:40 2016 +0000 @@ -39,10 +39,7 @@ if proxies is not None: opener = urllib.request.FancyURLopener(proxies=proxies) elif not _urlopener: - with support.check_warnings( - ('FancyURLopener style of invoking requests is deprecated.', - DeprecationWarning)): - opener = urllib.request.FancyURLopener() + opener = FancyURLopener() _urlopener = opener else: opener = _urlopener @@ -52,6 +49,13 @@ return opener.open(url, data) +def FancyURLopener(): + with support.check_warnings( + ('FancyURLopener style of invoking requests is deprecated.', + DeprecationWarning)): + return urllib.request.FancyURLopener() + + def fakehttp(fakedata): class FakeSocket(io.BytesIO): io_refs = 1 @@ -291,11 +295,26 @@ Content-Type: text/html; charset=iso-8859-1 ''') try: - self.assertRaises(urllib.error.HTTPError, urlopen, - "http://python.org/") + msg = "Redirection to url 'file:" + with self.assertRaisesRegex(urllib.error.HTTPError, msg): + 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(FancyURLopener().maxtries): + self.fakehttp(b'''HTTP/1.1 302 Found +Location: file://guidocomputer.athome.com:/python/license +Connection: close +''') + try: + self.assertRaises(urllib.error.HTTPError, urlopen, + "http://something") + finally: + self.unfakehttp() + def test_empty_socket(self): # urlopen() raises OSError if the underlying socket does not send any # data. (#1680230) diff -r d00a1c46fff7 -r d3be5c4507b4 Lib/urllib/request.py --- a/Lib/urllib/request.py Thu Feb 04 14:08:23 2016 -0500 +++ b/Lib/urllib/request.py Sat Feb 06 01:08:40 2016 +0000 @@ -2110,18 +2110,20 @@ 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: diff -r d00a1c46fff7 -r d3be5c4507b4 Misc/ACKS --- a/Misc/ACKS Thu Feb 04 14:08:23 2016 -0500 +++ b/Misc/ACKS Sat Feb 06 01:08:40 2016 +0000 @@ -1218,6 +1218,7 @@ Mark Roberts Andy Robinson Jim Robinson +Daniel Rocco Mark Roddy Kevin Rodgers Sean Rodman diff -r d00a1c46fff7 -r d3be5c4507b4 Misc/NEWS --- a/Misc/NEWS Thu Feb 04 14:08:23 2016 -0500 +++ b/Misc/NEWS Sat Feb 06 01:08:40 2016 +0000 @@ -169,6 +169,9 @@ 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