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

Skip to content

Commit 0a6b9ec

Browse files
committed
merge from 3.2
Fix Issue15701 - HTTPError info method call raises AttributeError. Fix that to return headers correctly
2 parents e3dff55 + 41e66a2 commit 0a6b9ec

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

Lib/test/test_urllib2.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,18 +1495,34 @@ def test_url_fragment(self):
14951495
req = Request(url)
14961496
self.assertEqual(req.get_full_url(), url)
14971497

1498-
def test_HTTPError_interface():
1499-
"""
1500-
Issue 13211 reveals that HTTPError didn't implement the URLError
1501-
interface even though HTTPError is a subclass of URLError.
1502-
1503-
>>> msg = 'something bad happened'
1504-
>>> url = code = hdrs = fp = None
1505-
>>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
1506-
>>> assert hasattr(err, 'reason')
1507-
>>> err.reason
1508-
'something bad happened'
1509-
"""
1498+
def test_HTTPError_interface(self):
1499+
"""
1500+
Issue 13211 reveals that HTTPError didn't implement the URLError
1501+
interface even though HTTPError is a subclass of URLError.
1502+
1503+
>>> msg = 'something bad happened'
1504+
>>> url = code = hdrs = fp = None
1505+
>>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
1506+
>>> assert hasattr(err, 'reason')
1507+
>>> err.reason
1508+
'something bad happened'
1509+
"""
1510+
1511+
def test_HTTPError_interface_call(self):
1512+
"""
1513+
Issue 15701 - HTTPError interface has info method available from URLError
1514+
"""
1515+
err = urllib.request.HTTPError(msg="something bad happened", url=None,
1516+
code=None, hdrs='Content-Length:42', fp=None)
1517+
self.assertTrue(hasattr(err, 'reason'))
1518+
assert hasattr(err, 'reason')
1519+
assert hasattr(err, 'info')
1520+
assert callable(err.info)
1521+
try:
1522+
err.info()
1523+
except AttributeError:
1524+
self.fail('err.info call failed.')
1525+
self.assertEqual(err.info(), "Content-Length:42")
15101526

15111527
def test_main(verbose=None):
15121528
from test import test_urllib2

Lib/urllib/error.py

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

64+
def info(self):
65+
return self.hdrs
66+
67+
6468
# exception raised when downloaded size does not match content-length
6569
class ContentTooShortError(URLError):
6670
def __init__(self, message, content):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Core and Builtins
114114
Library
115115
-------
116116

117+
- Issue #15701: Fix HTTPError info method call to return the headers information.
118+
117119
- Issue #16752: Add a missing import to modulefinder. Patch by Berker Peksag.
118120

119121
- Issue #16646: ftplib.FTP.makeport() might lose socket error details.

0 commit comments

Comments
 (0)