Use modern Python exception syntax

"except Exception as e" instead of "except Exception, e"

This is part of a transition to supporting Python 3.  Python >= 2.6
support "as" syntax.

Note: this removes Python 2.5 support.

Change-Id: I309599f3981bba2b46111c43102bee38ff132803
This commit is contained in:
Sarah Owens 2012-09-09 15:37:57 -07:00
parent 9ed12c5d9c
commit a5be53f9c8
10 changed files with 32 additions and 32 deletions

View File

@ -91,7 +91,7 @@ least one of these before using this command."""
try: try:
rc = subprocess.Popen(args, shell=shell).wait() rc = subprocess.Popen(args, shell=shell).wait()
except OSError, e: except OSError as e:
raise EditorError('editor failed, %s: %s %s' raise EditorError('editor failed, %s: %s %s'
% (str(e), editor, path)) % (str(e), editor, path))
if rc != 0: if rc != 0:

View File

@ -217,7 +217,7 @@ class GitCommand(object):
stdin = stdin, stdin = stdin,
stdout = stdout, stdout = stdout,
stderr = stderr) stderr = stderr)
except Exception, e: except Exception as e:
raise GitError('%s: %s' % (command[1], e)) raise GitError('%s: %s' % (command[1], e))
if ssh_proxy: if ssh_proxy:

View File

@ -449,7 +449,7 @@ def _open_ssh(host, port=None):
try: try:
Trace(': %s', ' '.join(command)) Trace(': %s', ' '.join(command))
p = subprocess.Popen(command) p = subprocess.Popen(command)
except Exception, e: except Exception as e:
_ssh_master = False _ssh_master = False
print >>sys.stderr, \ print >>sys.stderr, \
'\nwarn: cannot enable ssh control master for %s:%s\n%s' \ '\nwarn: cannot enable ssh control master for %s:%s\n%s' \
@ -592,9 +592,9 @@ class Remote(object):
else: else:
host, port = info.split() host, port = info.split()
self._review_url = self._SshReviewUrl(userEmail, host, port) self._review_url = self._SshReviewUrl(userEmail, host, port)
except urllib2.HTTPError, e: except urllib2.HTTPError as e:
raise UploadError('%s: %s' % (self.review, str(e))) raise UploadError('%s: %s' % (self.review, str(e)))
except urllib2.URLError, e: except urllib2.URLError as e:
raise UploadError('%s: %s' % (self.review, str(e))) raise UploadError('%s: %s' % (self.review, str(e)))
REVIEW_CACHE[u] = self._review_url REVIEW_CACHE[u] = self._review_url

10
main.py
View File

@ -146,13 +146,13 @@ class _Repo(object):
else: else:
print >>sys.stderr, 'real\t%dh%dm%.3fs' \ print >>sys.stderr, 'real\t%dh%dm%.3fs' \
% (hours, minutes, seconds) % (hours, minutes, seconds)
except DownloadError, e: except DownloadError as e:
print >>sys.stderr, 'error: %s' % str(e) print >>sys.stderr, 'error: %s' % str(e)
return 1 return 1
except ManifestInvalidRevisionError, e: except ManifestInvalidRevisionError as e:
print >>sys.stderr, 'error: %s' % str(e) print >>sys.stderr, 'error: %s' % str(e)
return 1 return 1
except NoSuchProjectError, e: except NoSuchProjectError as e:
if e.name: if e.name:
print >>sys.stderr, 'error: project %s not found' % e.name print >>sys.stderr, 'error: project %s not found' % e.name
else: else:
@ -390,14 +390,14 @@ def _Main(argv):
close_ssh() close_ssh()
except KeyboardInterrupt: except KeyboardInterrupt:
result = 1 result = 1
except RepoChangedException, rce: except RepoChangedException as rce:
# If repo changed, re-exec ourselves. # If repo changed, re-exec ourselves.
# #
argv = list(sys.argv) argv = list(sys.argv)
argv.extend(rce.extra_args) argv.extend(rce.extra_args)
try: try:
os.execv(__file__, argv) os.execv(__file__, argv)
except OSError, e: except OSError as e:
print >>sys.stderr, 'fatal: cannot restart repo after upgrade' print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
print >>sys.stderr, 'fatal: %s' % e print >>sys.stderr, 'fatal: %s' % e
result = 128 result = 128

View File

@ -336,7 +336,7 @@ class XmlManifest(object):
# tricky. actual parsing implementation may vary. # tricky. actual parsing implementation may vary.
except (KeyboardInterrupt, RuntimeError, SystemExit): except (KeyboardInterrupt, RuntimeError, SystemExit):
raise raise
except Exception, e: except Exception as e:
raise ManifestParseError( raise ManifestParseError(
"failed parsing included manifest %s: %s", (name, e)) "failed parsing included manifest %s: %s", (name, e))
else: else:

View File

@ -1044,7 +1044,7 @@ class Project(object):
try: try:
self._Checkout(revid, quiet=True) self._Checkout(revid, quiet=True)
except GitError, e: except GitError as e:
syncbuf.fail(self, e) syncbuf.fail(self, e)
return return
self._CopyFiles() self._CopyFiles()
@ -1066,7 +1066,7 @@ class Project(object):
branch.name) branch.name)
try: try:
self._Checkout(revid, quiet=True) self._Checkout(revid, quiet=True)
except GitError, e: except GitError as e:
syncbuf.fail(self, e) syncbuf.fail(self, e)
return return
self._CopyFiles() self._CopyFiles()
@ -1151,7 +1151,7 @@ class Project(object):
try: try:
self._ResetHard(revid) self._ResetHard(revid)
self._CopyFiles() self._CopyFiles()
except GitError, e: except GitError as e:
syncbuf.fail(self, e) syncbuf.fail(self, e)
return return
else: else:
@ -1723,7 +1723,7 @@ class Project(object):
continue continue
try: try:
os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst) os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst)
except OSError, e: except OSError as e:
if e.errno == errno.EPERM: if e.errno == errno.EPERM:
raise GitError('filesystem must support symlinks') raise GitError('filesystem must support symlinks')
else: else:
@ -1786,7 +1786,7 @@ class Project(object):
os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst) os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst)
else: else:
raise GitError('cannot overwrite a local work tree') raise GitError('cannot overwrite a local work tree')
except OSError, e: except OSError as e:
if e.errno == errno.EPERM: if e.errno == errno.EPERM:
raise GitError('filesystem must support symlinks') raise GitError('filesystem must support symlinks')
else: else:

20
repo
View File

@ -185,7 +185,7 @@ def _Init(args):
if not os.path.isdir(repodir): if not os.path.isdir(repodir):
try: try:
os.mkdir(repodir) os.mkdir(repodir)
except OSError, e: except OSError as e:
print >>sys.stderr, \ print >>sys.stderr, \
'fatal: cannot make %s directory: %s' % ( 'fatal: cannot make %s directory: %s' % (
repodir, e.strerror) repodir, e.strerror)
@ -221,7 +221,7 @@ def _CheckGitVersion():
cmd = [GIT, '--version'] cmd = [GIT, '--version']
try: try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
except OSError, e: except OSError as e:
print >>sys.stderr print >>sys.stderr
print >>sys.stderr, "fatal: '%s' is not available" % GIT print >>sys.stderr, "fatal: '%s' is not available" % GIT
print >>sys.stderr, 'fatal: %s' % e print >>sys.stderr, 'fatal: %s' % e
@ -268,7 +268,7 @@ def _SetupGnuPG(quiet):
if not os.path.isdir(home_dot_repo): if not os.path.isdir(home_dot_repo):
try: try:
os.mkdir(home_dot_repo) os.mkdir(home_dot_repo)
except OSError, e: except OSError as e:
print >>sys.stderr, \ print >>sys.stderr, \
'fatal: cannot make %s directory: %s' % ( 'fatal: cannot make %s directory: %s' % (
home_dot_repo, e.strerror) home_dot_repo, e.strerror)
@ -277,7 +277,7 @@ def _SetupGnuPG(quiet):
if not os.path.isdir(gpg_dir): if not os.path.isdir(gpg_dir):
try: try:
os.mkdir(gpg_dir, 0700) os.mkdir(gpg_dir, 0700)
except OSError, e: except OSError as e:
print >>sys.stderr, \ print >>sys.stderr, \
'fatal: cannot make %s directory: %s' % ( 'fatal: cannot make %s directory: %s' % (
gpg_dir, e.strerror) gpg_dir, e.strerror)
@ -291,7 +291,7 @@ def _SetupGnuPG(quiet):
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
env = env, env = env,
stdin = subprocess.PIPE) stdin = subprocess.PIPE)
except OSError, e: except OSError as e:
if not quiet: if not quiet:
print >>sys.stderr, 'warning: gpg (GnuPG) is not available.' print >>sys.stderr, 'warning: gpg (GnuPG) is not available.'
print >>sys.stderr, 'warning: Installing it is strongly encouraged.' print >>sys.stderr, 'warning: Installing it is strongly encouraged.'
@ -392,13 +392,13 @@ def _DownloadBundle(url, local, quiet):
try: try:
try: try:
r = urllib2.urlopen(url) r = urllib2.urlopen(url)
except urllib2.HTTPError, e: except urllib2.HTTPError as e:
if e.code == 404: if e.code == 404:
return False return False
print >>sys.stderr, 'fatal: Cannot get %s' % url print >>sys.stderr, 'fatal: Cannot get %s' % url
print >>sys.stderr, 'fatal: HTTP error %s' % e.code print >>sys.stderr, 'fatal: HTTP error %s' % e.code
raise CloneFailure() raise CloneFailure()
except urllib2.URLError, e: except urllib2.URLError as e:
print >>sys.stderr, 'fatal: Cannot get %s' % url print >>sys.stderr, 'fatal: Cannot get %s' % url
print >>sys.stderr, 'fatal: error %s' % e.reason print >>sys.stderr, 'fatal: error %s' % e.reason
raise CloneFailure() raise CloneFailure()
@ -427,7 +427,7 @@ def _Clone(url, local, quiet):
""" """
try: try:
os.mkdir(local) os.mkdir(local)
except OSError, e: except OSError as e:
print >>sys.stderr, \ print >>sys.stderr, \
'fatal: cannot make %s directory: %s' \ 'fatal: cannot make %s directory: %s' \
% (local, e.strerror) % (local, e.strerror)
@ -436,7 +436,7 @@ def _Clone(url, local, quiet):
cmd = [GIT, 'init', '--quiet'] cmd = [GIT, 'init', '--quiet']
try: try:
proc = subprocess.Popen(cmd, cwd = local) proc = subprocess.Popen(cmd, cwd = local)
except OSError, e: except OSError as e:
print >>sys.stderr print >>sys.stderr
print >>sys.stderr, "fatal: '%s' is not available" % GIT print >>sys.stderr, "fatal: '%s' is not available" % GIT
print >>sys.stderr, 'fatal: %s' % e print >>sys.stderr, 'fatal: %s' % e
@ -699,7 +699,7 @@ def main(orig_args):
me.extend(extra_args) me.extend(extra_args)
try: try:
os.execv(repo_main, me) os.execv(repo_main, me)
except OSError, e: except OSError as e:
print >>sys.stderr, "fatal: unable to start %s" % repo_main print >>sys.stderr, "fatal: unable to start %s" % repo_main
print >>sys.stderr, "fatal: %s" % e print >>sys.stderr, "fatal: %s" % e
sys.exit(148) sys.exit(148)

View File

@ -207,7 +207,7 @@ to update the working directory files.
try: try:
self.manifest.Link(name) self.manifest.Link(name)
except ManifestParseError, e: except ManifestParseError as e:
print >>sys.stderr, "fatal: manifest '%s' not available" % name print >>sys.stderr, "fatal: manifest '%s' not available" % name
print >>sys.stderr, 'fatal: %s' % str(e) print >>sys.stderr, 'fatal: %s' % str(e)
sys.exit(1) sys.exit(1)

View File

@ -417,7 +417,7 @@ uncommitted changes are present' % project.relpath
# in the .netrc file. # in the .netrc file.
print >>sys.stderr, 'No credentials found for %s in .netrc' % \ print >>sys.stderr, 'No credentials found for %s in .netrc' % \
parse_result.hostname parse_result.hostname
except netrc.NetrcParseError, e: except netrc.NetrcParseError as e:
print >>sys.stderr, 'Error parsing .netrc file: %s' % e print >>sys.stderr, 'Error parsing .netrc file: %s' % e
if (username and password): if (username and password):
@ -464,11 +464,11 @@ uncommitted changes are present' % project.relpath
else: else:
print >>sys.stderr, 'error: %s' % manifest_str print >>sys.stderr, 'error: %s' % manifest_str
sys.exit(1) sys.exit(1)
except (socket.error, IOError, xmlrpclib.Fault), e: except (socket.error, IOError, xmlrpclib.Fault) as e:
print >>sys.stderr, 'error: cannot connect to manifest server %s:\n%s' % ( print >>sys.stderr, 'error: cannot connect to manifest server %s:\n%s' % (
self.manifest.manifest_server, e) self.manifest.manifest_server, e)
sys.exit(1) sys.exit(1)
except xmlrpclib.ProtocolError, e: except xmlrpclib.ProtocolError as e:
print >>sys.stderr, 'error: cannot connect to manifest server %s:\n%d %s' % ( print >>sys.stderr, 'error: cannot connect to manifest server %s:\n%d %s' % (
self.manifest.manifest_server, e.errcode, e.errmsg) self.manifest.manifest_server, e.errcode, e.errmsg)
sys.exit(1) sys.exit(1)

View File

@ -329,7 +329,7 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
branch.UploadForReview(people, auto_topic=opt.auto_topic, draft=opt.draft) branch.UploadForReview(people, auto_topic=opt.auto_topic, draft=opt.draft)
branch.uploaded = True branch.uploaded = True
except UploadError, e: except UploadError as e:
branch.error = e branch.error = e
branch.uploaded = False branch.uploaded = False
have_errors = True have_errors = True
@ -384,7 +384,7 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
pending_proj_names = [project.name for (project, avail) in pending] pending_proj_names = [project.name for (project, avail) in pending]
try: try:
hook.Run(opt.allow_all_hooks, project_list=pending_proj_names) hook.Run(opt.allow_all_hooks, project_list=pending_proj_names)
except HookError, e: except HookError as e:
print >>sys.stderr, "ERROR: %s" % str(e) print >>sys.stderr, "ERROR: %s" % str(e)
return return