(original) (raw)
changeset: 73823:abfe76a19f63 branch: 3.2 parent: 73820:0e2812b16f5f user: Jason R. Coombs jaraco@jaraco.com date: Mon Nov 07 10:50:32 2011 -0500 files: Lib/test/test_urllib2.py Lib/urllib/error.py description: Issue #13211: Add .reason attribute to HTTPError to implement parent class (URLError) interface. diff -r 0e2812b16f5f -r abfe76a19f63 Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Sat Dec 03 08:24:21 2011 -0500 +++ b/Lib/test/test_urllib2.py Mon Nov 07 10:50:32 2011 -0500 @@ -1409,6 +1409,17 @@ req = Request(url) self.assertEqual(req.get_full_url(), url) +def test_HTTPError_interface(): + """ + Issue 13211 reveals that HTTPError didn't implement the URLError + interface even though HTTPError is a subclass of URLError. + + >>> err = urllib.error.HTTPError(msg='something bad happened', url=None, code=None, hdrs=None, fp=None) + >>> assert hasattr(err, 'reason') + >>> err.reason + 'something bad happened' + """ + def test_main(verbose=None): from test import test_urllib2 support.run_doctest(test_urllib2, verbose) diff -r 0e2812b16f5f -r abfe76a19f63 Lib/urllib/error.py --- a/Lib/urllib/error.py Sat Dec 03 08:24:21 2011 -0500 +++ b/Lib/urllib/error.py Mon Nov 07 10:50:32 2011 -0500 @@ -52,6 +52,12 @@ def __str__(self): return 'HTTP Error %s: %s' % (self.code, self.msg) + # since URLError specifies a .reason attribute, HTTPError should also + # provide this attribute. See issue13211 for discussion. + @property + def reason(self): + return self.msg + # exception raised when downloaded size does not match content-length class ContentTooShortError(URLError): def __init__(self, message, content): /jaraco@jaraco.com