Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a037022

Browse files
committed
Issue #12923: Reset FancyURLopener's redirect counter even on exception
Based on patches by Brian Brazil and Daniel Rocco.
1 parent 46f7785 commit a037022

4 files changed

Lines changed: 42 additions & 17 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ def urlopen(url, data=None, proxies=None):
3939
if proxies is not None:
4040
opener = urllib.request.FancyURLopener(proxies=proxies)
4141
elif not _urlopener:
42-
with support.check_warnings(
43-
('FancyURLopener style of invoking requests is deprecated.',
44-
DeprecationWarning)):
45-
opener = urllib.request.FancyURLopener()
42+
opener = FancyURLopener()
4643
_urlopener = opener
4744
else:
4845
opener = _urlopener
@@ -52,6 +49,13 @@ def urlopen(url, data=None, proxies=None):
5249
return opener.open(url, data)
5350

5451

52+
def FancyURLopener():
53+
with support.check_warnings(
54+
('FancyURLopener style of invoking requests is deprecated.',
55+
DeprecationWarning)):
56+
return urllib.request.FancyURLopener()
57+
58+
5559
def fakehttp(fakedata):
5660
class FakeSocket(io.BytesIO):
5761
io_refs = 1
@@ -291,11 +295,26 @@ def test_invalid_redirect(self):
291295
Content-Type: text/html; charset=iso-8859-1
292296
''')
293297
try:
294-
self.assertRaises(urllib.error.HTTPError, urlopen,
295-
"http://python.org/")
298+
msg = "Redirection to url 'file:"
299+
with self.assertRaisesRegex(urllib.error.HTTPError, msg):
300+
urlopen("http://python.org/")
296301
finally:
297302
self.unfakehttp()
298303

304+
def test_redirect_limit_independent(self):
305+
# Ticket #12923: make sure independent requests each use their
306+
# own retry limit.
307+
for i in range(FancyURLopener().maxtries):
308+
self.fakehttp(b'''HTTP/1.1 302 Found
309+
Location: file://guidocomputer.athome.com:/python/license
310+
Connection: close
311+
''')
312+
try:
313+
self.assertRaises(urllib.error.HTTPError, urlopen,
314+
"http://something")
315+
finally:
316+
self.unfakehttp()
317+
299318
def test_empty_socket(self):
300319
# urlopen() raises OSError if the underlying socket does not send any
301320
# data. (#1680230)

Lib/urllib/request.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,18 +2050,20 @@ def http_error_default(self, url, fp, errcode, errmsg, headers):
20502050
def http_error_302(self, url, fp, errcode, errmsg, headers, data=None):
20512051
"""Error 302 -- relocated (temporarily)."""
20522052
self.tries += 1
2053-
if self.maxtries and self.tries >= self.maxtries:
2054-
if hasattr(self, "http_error_500"):
2055-
meth = self.http_error_500
2056-
else:
2057-
meth = self.http_error_default
2053+
try:
2054+
if self.maxtries and self.tries >= self.maxtries:
2055+
if hasattr(self, "http_error_500"):
2056+
meth = self.http_error_500
2057+
else:
2058+
meth = self.http_error_default
2059+
return meth(url, fp, 500,
2060+
"Internal Server Error: Redirect Recursion",
2061+
headers)
2062+
result = self.redirect_internal(url, fp, errcode, errmsg,
2063+
headers, data)
2064+
return result
2065+
finally:
20582066
self.tries = 0
2059-
return meth(url, fp, 500,
2060-
"Internal Server Error: Redirect Recursion", headers)
2061-
result = self.redirect_internal(url, fp, errcode, errmsg, headers,
2062-
data)
2063-
self.tries = 0
2064-
return result
20652067

20662068
def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
20672069
if 'location' in headers:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ Ben Roberts
12141214
Mark Roberts
12151215
Andy Robinson
12161216
Jim Robinson
1217+
Daniel Rocco
12171218
Mark Roddy
12181219
Kevin Rodgers
12191220
Sean Rodman

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Core and Builtins
7373
Library
7474
-------
7575

76+
- Issue #12923: Reset FancyURLopener's redirect counter even if there is an
77+
exception. Based on patches by Brian Brazil and Daniel Rocco.
78+
7679
- Issue #25945: Fixed a crash when unpickle the functools.partial object with
7780
wrong state. Fixed a leak in failed functools.partial constructor.
7881
"args" and "keywords" attributes of functools.partial have now always types

0 commit comments

Comments
 (0)