Fix Issue15701 - HTTPError info method call raises AttributeError. Fix that to return...
authorSenthil Kumaran <senthil@uthcode.com>
Sun, 23 Dec 2012 17:00:47 +0000 (09:00 -0800)
committerSenthil Kumaran <senthil@uthcode.com>
Sun, 23 Dec 2012 17:00:47 +0000 (09:00 -0800)
Lib/test/test_urllib2.py
Lib/urllib2.py
Misc/NEWS

index 12263b381dc42718a2bceaac295fcda2eea592ab..eac995001e8b1707bda2d08d4a59f4dba569f1e9 100644 (file)
@@ -1336,16 +1336,32 @@ 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_HTTPError_interface(self):
+        """
+        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_HTTPError_interface_call(self):
+        """
+        Issue 15701= - HTTPError interface has info method available from URLError.
+        """
+        err = urllib2.HTTPError(msg='something bad happened', url=None,
+                                code=None, hdrs='Content-Length:42', fp=None)
+        self.assertTrue(hasattr(err, 'reason'))
+        assert hasattr(err, 'reason')
+        assert hasattr(err, 'info')
+        assert callable(err.info)
+        try:
+            err.info()
+        except AttributeError:
+            self.fail("err.info() failed")
+        self.assertEqual(err.info(), "Content-Length:42")
 
 def test_main(verbose=None):
     from test import test_urllib2
index ebf58119d6ade6f0923e5813c0432f44b3d49f93..aadeb7371ebc694ff6c230a38f9c6c8c9425f58c 100644 (file)
@@ -173,6 +173,9 @@ class HTTPError(URLError, addinfourl):
     def reason(self):
         return self.msg
 
+    def info(self):
+        return self.hdrs
+
 # copied from cookielib.py
 _cut_port_re = re.compile(r":\d+$")
 def request_host(request):
index babe8383a27aac0a25d13013ab838089579d4380..c0172572a7fd18596ead3cc1d05f9a8aaf8570fc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -165,6 +165,8 @@ Library
 - Issue #16597: In buffered and text IO, call close() on the underlying stream
   if invoking flush() fails.
 
+- Issue #15701: Fix HTTPError info method call to return the headers information.
+
 - Issue #16646: ftplib.FTP.makeport() might lose socket error details.
   (patch by Serhiy Storchaka)