]> granicus.if.org Git - python/commitdiff
Fix Issue15701 - HTTPError info method call raises AttributeError. Fix that to return...
authorSenthil Kumaran <senthil@uthcode.com>
Sun, 23 Dec 2012 17:04:24 +0000 (09:04 -0800)
committerSenthil Kumaran <senthil@uthcode.com>
Sun, 23 Dec 2012 17:04:24 +0000 (09:04 -0800)
Lib/test/test_urllib2.py
Lib/urllib/error.py
Misc/NEWS

index d8c302b712985a12ed2b0b83c2a5a61ec7b7b9af..5eab30af76553cd00a1e7dffa4a21ba681a2feac 100644 (file)
@@ -1438,16 +1438,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 = 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_HTTPError_interface(self):
+        """
+        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_HTTPError_interface_call(self):
+        """
+        Issue 15701 - HTTPError interface has info method available from URLError
+        """
+        err = urllib.request.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 call failed.')
+        self.assertEqual(err.info(), "Content-Length:42")
 
 def test_main(verbose=None):
     from test import test_urllib2
index 40add4161a5a0c3935d6e6aa5054eac147437128..eb14c1d8b45d196ff5a9ac96c0fbf86e5cd905d2 100644 (file)
@@ -58,6 +58,10 @@ class HTTPError(URLError, urllib.response.addinfourl):
     def reason(self):
         return self.msg
 
+    def info(self):
+        return self.hdrs
+
+
 # exception raised when downloaded size does not match content-length
 class ContentTooShortError(URLError):
     def __init__(self, message, content):
index 2b50cbbccef12874851d772a3987314ec532d4f7..9a33632c7fdd4ef3a5adb6c5acdac26d978a9285 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -179,6 +179,8 @@ Core and Builtins
 Library
 -------
 
+- 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)