]> granicus.if.org Git - python/commitdiff
Fix Issue15701 : add .headers attribute to urllib.error.HTTPError
authorSenthil Kumaran <senthil@uthcode.com>
Mon, 10 Dec 2012 10:09:35 +0000 (02:09 -0800)
committerSenthil Kumaran <senthil@uthcode.com>
Mon, 10 Dec 2012 10:09:35 +0000 (02:09 -0800)
Doc/library/urllib.error.rst
Lib/test/test_urllib2.py
Lib/urllib/error.py
Misc/NEWS

index 9c3fe91118a667066842353a3408eef41adbf5c5..7bd04b1bc466da44ae9fcfe9ceb582e9d9d02efc 100644 (file)
@@ -45,6 +45,13 @@ The following exceptions are raised by :mod:`urllib.error` as appropriate:
 
       This is usually a string explaining the reason for this error.
 
+   .. attribute:: headers
+
+      The HTTP response headers for the HTTP request that cause the
+      :exc:`HTTPError`.
+
+      .. versionadded:: 3.4
+
 .. exception:: ContentTooShortError(msg, content)
 
    This exception is raised when the :func:`urlretrieve` function detects that
index 2261d5704757313fad3643a997253b811e913722..33042eda16311863d870de45a086c49ba4759306 100644 (file)
@@ -1539,11 +1539,15 @@ def test_HTTPError_interface():
     interface even though HTTPError is a subclass of URLError.
 
     >>> msg = 'something bad happened'
-    >>> url = code = hdrs = fp = None
+    >>> url = code = fp = None
+    >>> hdrs = 'Content-Length: 42'
     >>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
     >>> assert hasattr(err, 'reason')
     >>> err.reason
     'something bad happened'
+    >>> assert hasattr(err, 'headers')
+    >>> err.headers
+    'Content-Length: 42'
     """
 
 def test_main(verbose=None):
index c1dfc40ed7dc362126f7f605736bc48771dc55c7..237ed6b9bb83a6a846daf98bc778f5f2398ba802 100644 (file)
@@ -61,6 +61,14 @@ class HTTPError(URLError, urllib.response.addinfourl):
     def reason(self):
         return self.msg
 
+    @property
+    def headers(self):
+        return self.hdrs
+
+    @headers.setter
+    def headers(self, headers):
+        self.hdrs = headers
+
 # exception raised when downloaded size does not match content-length
 class ContentTooShortError(URLError):
     def __init__(self, message, content):
index cccb63e151834bb10faa56e3171c987f798324e2..2bad29ad9d0e9cf0adbbac7a3ac9416850e10b5c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -163,6 +163,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15701: Add a .headers attribute to urllib.error.HTTPError. Patch
+  contributed by Berker Peksag.
+
 - Issue #15872: Fix 3.3 regression introduced by the new fd-based shutil.rmtree
   that caused it to not ignore certain errors when ignore_errors was set.
   Patch by Alessandro Moura and Serhiy Storchaka.