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

Skip to content

Commit aa204db

Browse files
committed
Issue #13211: Add .reason attribute to HTTPError to implement parent class (URLError) interface.
1 parent a90e364 commit aa204db

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
@@ -1409,6 +1409,17 @@ def test_url_fragment(self):
14091409
req = Request(url)
14101410
self.assertEqual(req.get_full_url(), url)
14111411

1412+
def test_HTTPError_interface():
1413+
"""
1414+
Issue 13211 reveals that HTTPError didn't implement the URLError
1415+
interface even though HTTPError is a subclass of URLError.
1416+
1417+
>>> err = urllib.error.HTTPError(msg='something bad happened', url=None, code=None, hdrs=None, fp=None)
1418+
>>> assert hasattr(err, 'reason')
1419+
>>> err.reason
1420+
'something bad happened'
1421+
"""
1422+
14121423
def test_main(verbose=None):
14131424
from test import test_urllib2
14141425
support.run_doctest(test_urllib2, verbose)

Lib/urllib/error.py

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

55+
# since URLError specifies a .reason attribute, HTTPError should also
56+
# provide this attribute. See issue13211 for discussion.
57+
@property
58+
def reason(self):
59+
return self.msg
60+
5561
# exception raised when downloaded size does not match content-length
5662
class ContentTooShortError(URLError):
5763
def __init__(self, message, content):

0 commit comments

Comments
 (0)