]> 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:44:25 +0000 (10:44 -0500)
committerJason R. Coombs <jaraco@jaraco.com>
Mon, 7 Nov 2011 15:44:25 +0000 (10:44 -0500)
Lib/test/test_urllib2.py
Lib/urllib2.py

index e2473159dd8c44a3963195ed6d125101d2cfe44e..7f230e2d4aa3a1036db8b7ff7313d665533d504d 100644 (file)
@@ -1318,6 +1318,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 = urllib2.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
     test_support.run_doctest(test_urllib2, verbose)
index a0065b5b84df9f4c42dce3dd0eac91556b5044f0..5471acd403ec824b9b1b2cb4bfd043597085bce9 100644 (file)
@@ -166,6 +166,12 @@ class HTTPError(URLError, 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 fo discussion.
+    @property
+    def reason(self):
+        return self.msg
+
 # copied from cookielib.py
 _cut_port_re = re.compile(r":\d+$")
 def request_host(request):