cpython (3.2): Issue #13211: Add .reason attribute to HTTPError to implement parent class

http://hg.python.org/cpython/rev/abfe76a19f63 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 summary: Issue #13211: Add .reason attribute to HTTPError to implement parent class (URLError) interface. files: Lib/test/test_urllib2.py | 11 +++++++++++ Lib/urllib/error.py | 6 ++++++ 2 files changed, 17 insertions(+), 0 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -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 --git a/Lib/urllib/error.py b/Lib/urllib/error.py --- a/Lib/urllib/error.py +++ b/Lib/urllib/error.py @@ -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): -- Repository URL: http://hg.python.org/cpython

On Sun, Dec 4, 2011 at 12:46 AM, jason.coombs <python-checkins@python.org> wrote:
+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' + """ +
Did you re-run the test suite after forward-porting to 3.3? I'm consistently getting failures: $ ./python -m test test_urllib2 [1/1] test_urllib2 ********************************************************************** File "/home/ncoghlan/devel/py3k/Lib/test/test_urllib2.py", line 1457, in test.test_urllib2.test_HTTPError_interface Failed example: err = urllib.error.HTTPError(msg='something bad happened', url=None, code=None, hdrs=None, fp=None) Exception raised: Traceback (most recent call last): File "/home/ncoghlan/devel/py3k/Lib/doctest.py", line 1253, in __run compileflags, 1), test.globs) File "<doctest test.test_urllib2.test_HTTPError_interface[0]>", line 1, in <module> err = urllib.error.HTTPError(msg='something bad happened', url=None, code=None, hdrs=None, fp=None) TypeError: HTTPError does not take keyword arguments ********************************************************************** File "/home/ncoghlan/devel/py3k/Lib/test/test_urllib2.py", line 1458, in test.test_urllib2.test_HTTPError_interface Failed example: assert hasattr(err, 'reason') Exception raised: Traceback (most recent call last): File "/home/ncoghlan/devel/py3k/Lib/doctest.py", line 1253, in __run compileflags, 1), test.globs) File "<doctest test.test_urllib2.test_HTTPError_interface[1]>", line 1, in <module> assert hasattr(err, 'reason') NameError: name 'err' is not defined ********************************************************************** File "/home/ncoghlan/devel/py3k/Lib/test/test_urllib2.py", line 1459, in test.test_urllib2.test_HTTPError_interface Failed example: err.reason Exception raised: Traceback (most recent call last): File "/home/ncoghlan/devel/py3k/Lib/doctest.py", line 1253, in __run compileflags, 1), test.globs) File "<doctest test.test_urllib2.test_HTTPError_interface[2]>", line 1, in <module> err.reason NameError: name 'err' is not defined ********************************************************************** 1 items had failures: 3 of 3 in test.test_urllib2.test_HTTPError_interface ***Test Failed*** 3 failures. test test_urllib2 failed -- 3 of 65 doctests failed 1 test failed: test_urllib2 [142313 refs] Now, this failure is quite possibly due to a flaw in the PEP 3151 implementation (see http://bugs.python.org/issue12555), but picking up this kind of thing is the reason we say to always run the tests before committing, even for a simple merge. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (2)
-
jason.coombs
-
Nick Coghlan