diff options
author | Shawn O. Pearce <sop@google.com> | 2011-10-11 09:24:07 -0700 |
---|---|---|
committer | Shawn O. Pearce <sop@google.com> | 2011-10-11 09:24:07 -0700 |
commit | 29472463ba601e9c0513eadb19470e435b2601a1 (patch) | |
tree | 5dc815bb61bd53c60fd301f429d779994ef07f9b /project.py | |
parent | c325dc35f621fe24d0460bb14547cdb51e00c00b (diff) | |
download | git-repo-29472463ba601e9c0513eadb19470e435b2601a1.tar.gz |
Work around Python 2.7 failure to initialize base class
urllib2 returns a malformed HTTPError object in certain situations.
For example, urllib2 has a couple of places where it creates an
HTTPError object with no fp:
if self.retried > 5:
# retry sending the username:password 5 times before failing.
raise HTTPError(req.get_full_url(), 401, "basic auth failed",
headers, None)
When it does that, HTTPError's ctor doesn't call through to
addinfourl's ctor:
# The addinfourl classes depend on fp being a valid file
# object. In some cases, the HTTPError may not have a valid
# file object. If this happens, the simplest workaround is to
# not initialize the base classes.
if fp is not None:
self.__super_init(fp, hdrs, url, code)
Which means the 'headers' slot in addinfourl is not initialized and
info() fails. It is completely insane that urllib2 decides not to
initialize its own base class sometimes.
Change-Id: I32a0d738f71bdd7d38d86078b71d9001e26f1ec3
Signed-off-by: Shawn O. Pearce <sop@google.com>
Diffstat (limited to 'project.py')
-rw-r--r-- | project.py | 8 |
1 files changed, 7 insertions, 1 deletions
@@ -1465,10 +1465,16 @@ class Project(object): | |||
1465 | try: | 1465 | try: |
1466 | r = urllib2.urlopen(req) | 1466 | r = urllib2.urlopen(req) |
1467 | except urllib2.HTTPError, e: | 1467 | except urllib2.HTTPError, e: |
1468 | def _content_type(): | ||
1469 | try: | ||
1470 | return e.info()['content-type'] | ||
1471 | except: | ||
1472 | return None | ||
1473 | |||
1468 | if e.code == 404: | 1474 | if e.code == 404: |
1469 | keep = False | 1475 | keep = False |
1470 | return False | 1476 | return False |
1471 | elif e.info()['content-type'] == 'text/plain': | 1477 | elif _content_type() == 'text/plain': |
1472 | try: | 1478 | try: |
1473 | msg = e.read() | 1479 | msg = e.read() |
1474 | if len(msg) > 0 and msg[-1] == '\n': | 1480 | if len(msg) > 0 and msg[-1] == '\n': |