[Python-checkins] cpython: Fix Issue15701 : add .headers attribute to urllib.error.HTTPError

senthil.kumaran python-checkins at python.org
Mon Dec 10 11:09:48 CET 2012


http://hg.python.org/cpython/rev/ad1c1164f68b
changeset:   80805:ad1c1164f68b
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Mon Dec 10 02:09:35 2012 -0800
summary:
  Fix Issue15701 : add .headers attribute to urllib.error.HTTPError

files:
  Doc/library/urllib.error.rst |  7 +++++++
  Lib/test/test_urllib2.py     |  6 +++++-
  Lib/urllib/error.py          |  8 ++++++++
  Misc/NEWS                    |  3 +++
  4 files changed, 23 insertions(+), 1 deletions(-)


diff --git a/Doc/library/urllib.error.rst b/Doc/library/urllib.error.rst
--- a/Doc/library/urllib.error.rst
+++ b/Doc/library/urllib.error.rst
@@ -45,6 +45,13 @@
 
       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
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
@@ -1539,11 +1539,15 @@
     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):
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,14 @@
     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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -163,6 +163,9 @@
 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.

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


More information about the Python-checkins mailing list