[Python-checkins] cpython (2.7): Issue #13211: Add .reason attribute to HTTPError to implement parent class

jason.coombs python-checkins at python.org
Sat Dec 3 15:46:11 CET 2011


http://hg.python.org/cpython/rev/ee94b89f65ab
changeset:   73822:ee94b89f65ab
branch:      2.7
parent:      73815:6e03ab9950f6
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Mon Nov 07 10:44:25 2011 -0500
summary:
  Issue #13211: Add .reason attribute to HTTPError to implement parent class (URLError) interface.

files:
  Lib/test/test_urllib2.py |  11 +++++++++++
  Lib/urllib2.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
@@ -1318,6 +1318,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 = 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)
diff --git a/Lib/urllib2.py b/Lib/urllib2.py
--- a/Lib/urllib2.py
+++ b/Lib/urllib2.py
@@ -166,6 +166,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 fo discussion.
+    @property
+    def reason(self):
+        return self.msg
+
 # copied from cookielib.py
 _cut_port_re = re.compile(r":\d+$")
 def request_host(request):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list