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

Skip to content

Commit 0612e8c

Browse files
committed
Merged fix for #13211 from 3.2
2 parents c9b644e + aa204db commit 0612e8c

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lib/test/test_urllib2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,17 @@ def test_url_fragment(self):
14491449
req = Request(url)
14501450
self.assertEqual(req.get_full_url(), url)
14511451

1452+
def test_HTTPError_interface():
1453+
"""
1454+
Issue 13211 reveals that HTTPError didn't implement the URLError
1455+
interface even though HTTPError is a subclass of URLError.
1456+
1457+
>>> err = urllib.error.HTTPError(msg='something bad happened', url=None, code=None, hdrs=None, fp=None)
1458+
>>> assert hasattr(err, 'reason')
1459+
>>> err.reason
1460+
'something bad happened'
1461+
"""
1462+
14521463
def test_main(verbose=None):
14531464
from test import test_urllib2
14541465
support.run_doctest(test_urllib2, verbose)

Lib/urllib/error.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def __init__(self, url, code, msg, hdrs, fp):
5555
def __str__(self):
5656
return 'HTTP Error %s: %s' % (self.code, self.msg)
5757

58+
# since URLError specifies a .reason attribute, HTTPError should also
59+
# provide this attribute. See issue13211 for discussion.
60+
@property
61+
def reason(self):
62+
return self.msg
63+
5864
# exception raised when downloaded size does not match content-length
5965
class ContentTooShortError(URLError):
6066
def __init__(self, message, content):

0 commit comments

Comments
 (0)