Do HTTPError objects always have a read method?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Sep 19 00:03:10 EDT 2008


En Thu, 18 Sep 2008 23:12:22 -0300, Gabriel Genellina  
<gagsl-py2 at yahoo.com.ar> escribió:

> En Wed, 17 Sep 2008 22:48:27 -0300, Steven D'Aprano  
> <steven at remove.this.cybersource.com.au> escribió:
>
>> Under what circumstances do HTTPError objects not have a valid file
>> object? How common is this? Does anyone have an example of a URL that
>> fails in that fashion?
>
> Well, there is at least one case (AbstractDigestAuthHandler at line 864  
> in urllib2.py) where HTTPError is raised explicitely with fp=None. It's  
> not a common case, I think. If you rely on the exception having a read()  
> method, add it a fake one yourself:
>
> try: ...
> except urllib2.HTTPError, e:
>      if not hasattr(e, 'read'):
>          e.read = e.readline = lambda self: '' # or perhaps e.msg
>      conn = e

Oops, that should have been e.read = lambda: ''

-- 
Gabriel Genellina




More information about the Python-list mailing list