[Python-checkins] cpython (merge 3.2 -> 3.3): merge from 3.2

senthil.kumaran python-checkins at python.org
Sun Dec 23 18:14:00 CET 2012


http://hg.python.org/cpython/rev/a15109398294
changeset:   80998:a15109398294
branch:      3.3
parent:      80993:f05d29353f02
parent:      80997:919ebf74bfdb
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Sun Dec 23 09:12:13 2012 -0800
summary:
  merge from 3.2

Fix Issue15701 - HTTPError info method call raises AttributeError. Fix that to return headers correctly

files:
  Lib/test/test_urllib2.py |  38 +++++++++++++++++++--------
  Lib/urllib/error.py      |   4 ++
  Misc/NEWS                |   2 +
  3 files changed, 33 insertions(+), 11 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
@@ -1495,18 +1495,34 @@
         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.
+    def test_HTTPError_interface(self):
+        """
+        Issue 13211 reveals that HTTPError didn't implement the URLError
+        interface even though HTTPError is a subclass of URLError.
 
-    >>> msg = 'something bad happened'
-    >>> url = code = hdrs = fp = None
-    >>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
-    >>> assert hasattr(err, 'reason')
-    >>> err.reason
-    'something bad happened'
-    """
+        >>> msg = 'something bad happened'
+        >>> url = code = hdrs = fp = None
+        >>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
+        >>> 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
diff --git a/Lib/urllib/error.py b/Lib/urllib/error.py
--- a/Lib/urllib/error.py
+++ b/Lib/urllib/error.py
@@ -61,6 +61,10 @@
     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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -114,6 +114,8 @@
 Library
 -------
 
+- Issue #15701: Fix HTTPError info method call to return the headers information.
+
 - Issue #16752: Add a missing import to modulefinder. Patch by Berker Peksag.
 
 - Issue #16646: ftplib.FTP.makeport() might lose socket error details.

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


More information about the Python-checkins mailing list