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

Skip to content

Commit 40bbae3

Browse files
committed
Fix HTTPError __init__ for cases where fp is None.
The HTTPError class tries to act as a regular response objects for HTTP protocol errors that include full responses. If the failure is more basic, like no valid response, the __init__ choked when it tried to initialize its superclasses in addinfourl hierarchy that requires a valid response. The solution isn't elegant but seems to be effective. Do not initialize the base classes if there isn't a file object containing the response. In this case, user code expecting to use the addinfourl methods will fail; but it was going to fail anyway. It might be cleaner to factor out HTTPError into two classes, only one of which inherits from addinfourl. Not sure that the extra complexity would lead to any improved functionality, though. Partial fix for SF bug # 563665. Bug fix candidate for 2.1 and 2.2.
1 parent 65230a2 commit 40bbae3

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

Lib/urllib2.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,17 @@ class HTTPError(URLError, addinfourl):
157157
__super_init = addinfourl.__init__
158158

159159
def __init__(self, url, code, msg, hdrs, fp):
160-
self.__super_init(fp, hdrs, url)
161160
self.code = code
162161
self.msg = msg
163162
self.hdrs = hdrs
164163
self.fp = fp
165-
# XXX
166164
self.filename = url
165+
# The addinfourl classes depend on fp being a valid file
166+
# object. In some cases, the HTTPError may not have a valid
167+
# file object. If this happens, the simplest workaround is to
168+
# not initialize the base classes.
169+
if fp is not None:
170+
self.__super_init(fp, hdrs, url)
167171

168172
def __str__(self):
169173
return 'HTTP Error %s: %s' % (self.code, self.msg)

0 commit comments

Comments
 (0)