]> granicus.if.org Git - python/commitdiff
Issue #13211: Add .reason attribute to HTTPError to implement parent class (URLError...
authorJason R. Coombs <jaraco@jaraco.com>
Mon, 7 Nov 2011 15:50:32 +0000 (10:50 -0500)
committerJason R. Coombs <jaraco@jaraco.com>
Mon, 7 Nov 2011 15:50:32 +0000 (10:50 -0500)
Lib/test/test_urllib2.py
Lib/urllib/error.py

index 83bb0a9cb209eb5ce3dd341de889d451121fc6a3..c94ebf8165ea0c9a3980ff2ba66195b40a2a5dac 100644 (file)
@@ -1409,6 +1409,17 @@ class RequestTests(unittest.TestCase):
         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)
index 300c3fe16dd2db6741ae5cc857178a8bcce887f8..40add4161a5a0c3935d6e6aa5054eac147437128 100644 (file)
@@ -52,6 +52,12 @@ class HTTPError(URLError, urllib.response.addinfourl):
     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):