Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5962cce

Browse files
committed
Fix Issue15701 : add .headers attribute to urllib.error.HTTPError
1 parent 8e5a829 commit 5962cce

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

Doc/library/urllib.error.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ The following exceptions are raised by :mod:`urllib.error` as appropriate:
4545

4646
This is usually a string explaining the reason for this error.
4747

48+
.. attribute:: headers
49+
50+
The HTTP response headers for the HTTP request that cause the
51+
:exc:`HTTPError`.
52+
53+
.. versionadded:: 3.4
54+
4855
.. exception:: ContentTooShortError(msg, content)
4956

5057
This exception is raised when the :func:`urlretrieve` function detects that

Lib/test/test_urllib2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,11 +1539,15 @@ def test_HTTPError_interface():
15391539
interface even though HTTPError is a subclass of URLError.
15401540
15411541
>>> msg = 'something bad happened'
1542-
>>> url = code = hdrs = fp = None
1542+
>>> url = code = fp = None
1543+
>>> hdrs = 'Content-Length: 42'
15431544
>>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
15441545
>>> assert hasattr(err, 'reason')
15451546
>>> err.reason
15461547
'something bad happened'
1548+
>>> assert hasattr(err, 'headers')
1549+
>>> err.headers
1550+
'Content-Length: 42'
15471551
"""
15481552

15491553
def test_main(verbose=None):

Lib/urllib/error.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ def __str__(self):
6161
def reason(self):
6262
return self.msg
6363

64+
@property
65+
def headers(self):
66+
return self.hdrs
67+
68+
@headers.setter
69+
def headers(self, headers):
70+
self.hdrs = headers
71+
6472
# exception raised when downloaded size does not match content-length
6573
class ContentTooShortError(URLError):
6674
def __init__(self, message, content):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ Core and Builtins
163163
Library
164164
-------
165165

166+
- Issue #15701: Add a .headers attribute to urllib.error.HTTPError. Patch
167+
contributed by Berker Peksag.
168+
166169
- Issue #15872: Fix 3.3 regression introduced by the new fd-based shutil.rmtree
167170
that caused it to not ignore certain errors when ignore_errors was set.
168171
Patch by Alessandro Moura and Serhiy Storchaka.

0 commit comments

Comments
 (0)