This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author orsenthil
Recipients docs@python, jaraco, orsenthil, petri.lehtinen
Date 2011-11-08.17:39:07
SpamBayes Score 1.1607432e-10
Marked as misclassified No
Message-id <1320773948.49.0.733111145447.issue13211@psf.upfronthosting.co.za>
In-reply-to
Content
Hi Jason & Petri,

urllib2.HTTPError never had a reason attribute. In the docs, it is mentioned that only URLError has the reason attribute. The HTTPError sublasses URLError and addinforurl class, but the further initialization happens only in the addinforurl class atrs.

If you got confused that the HTTPError as args and not reason, the 'args' is not coming from URLError.

HTTPError is raised for peculiar conditions such like authentication failures and it is 'used' as expected failure for certain authentication conditions. URLError is not so, it is seen more as an exception like socket errors.

The example your illustrated is an Authentication failure and as per docs, it is guaranteed to have code attribute to verify the kind of HTTP error are getting. msg is corresponding HTTP error code msg.

Take this example for URLError which will have reason attribute, it will work in both 2.7,3.2 and 3.3

import urllib.request, urllib.error, urllib.parse

try:
    urllib.request.urlopen('http://aninvalidsite/something')
except urllib.error.URLError as exc:
    print(exc.reason)


Because this is a socket error, the reason as "[Errno -2] Name or service not known" and HTTPError may not be a proper exception for this. 

This is more of an IOError which urllib calls a URLError.

I am not sure, how the need for 'reason' attribute for HTTPError exception was felt as the docs just say about 'code'.  (HTTPError  is informed as a subclass of URLError, but HTTPError does not call the URLError 's  __init__ and acts standalone. 

I am not sure, how to go about with this bug. If a new .reason attribute has to be added to HTTPError, then it is a feature request though, I wonder why we need when code and msg serve an adequate purpose.
History
Date User Action Args
2011-11-08 17:39:08orsenthilsetrecipients: + orsenthil, jaraco, docs@python, petri.lehtinen
2011-11-08 17:39:08orsenthilsetmessageid: <1320773948.49.0.733111145447.issue13211@psf.upfronthosting.co.za>
2011-11-08 17:39:07orsenthillinkissue13211 messages
2011-11-08 17:39:07orsenthilcreate