From a5be53f9c809009e67f217c00b8f30246aacc237 Mon Sep 17 00:00:00 2001 From: Sarah Owens Date: Sun, 9 Sep 2012 15:37:57 -0700 Subject: [PATCH] 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 --- editor.py | 2 +- git_command.py | 2 +- git_config.py | 6 +++--- main.py | 10 +++++----- manifest_xml.py | 2 +- project.py | 10 +++++----- repo | 20 ++++++++++---------- subcmds/init.py | 2 +- subcmds/sync.py | 6 +++--- subcmds/upload.py | 4 ++-- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/editor.py b/editor.py index 62afbb91..489c6cd3 100644 --- a/editor.py +++ b/editor.py @@ -91,7 +91,7 @@ least one of these before using this command.""" try: rc = subprocess.Popen(args, shell=shell).wait() - except OSError, e: + except OSError as e: raise EditorError('editor failed, %s: %s %s' % (str(e), editor, path)) if rc != 0: diff --git a/git_command.py b/git_command.py index 82709b91..a40e6c05 100644 --- a/git_command.py +++ b/git_command.py @@ -217,7 +217,7 @@ class GitCommand(object): stdin = stdin, stdout = stdout, stderr = stderr) - except Exception, e: + except Exception as e: raise GitError('%s: %s' % (command[1], e)) if ssh_proxy: diff --git a/git_config.py b/git_config.py index afaa6f15..ae288558 100644 --- a/git_config.py +++ b/git_config.py @@ -449,7 +449,7 @@ def _open_ssh(host, port=None): try: Trace(': %s', ' '.join(command)) p = subprocess.Popen(command) - except Exception, e: + except Exception as e: _ssh_master = False print >>sys.stderr, \ '\nwarn: cannot enable ssh control master for %s:%s\n%s' \ @@ -592,9 +592,9 @@ class Remote(object): else: host, port = info.split() 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))) - except urllib2.URLError, e: + except urllib2.URLError as e: raise UploadError('%s: %s' % (self.review, str(e))) REVIEW_CACHE[u] = self._review_url diff --git a/main.py b/main.py index 278fd36f..d993ee4e 100755 --- a/main.py +++ b/main.py @@ -146,13 +146,13 @@ class _Repo(object): else: print >>sys.stderr, 'real\t%dh%dm%.3fs' \ % (hours, minutes, seconds) - except DownloadError, e: + except DownloadError as e: print >>sys.stderr, 'error: %s' % str(e) return 1 - except ManifestInvalidRevisionError, e: + except ManifestInvalidRevisionError as e: print >>sys.stderr, 'error: %s' % str(e) return 1 - except NoSuchProjectError, e: + except NoSuchProjectError as e: if e.name: print >>sys.stderr, 'error: project %s not found' % e.name else: @@ -390,14 +390,14 @@ def _Main(argv): close_ssh() except KeyboardInterrupt: result = 1 - except RepoChangedException, rce: + except RepoChangedException as rce: # If repo changed, re-exec ourselves. # argv = list(sys.argv) argv.extend(rce.extra_args) try: os.execv(__file__, argv) - except OSError, e: + except OSError as e: print >>sys.stderr, 'fatal: cannot restart repo after upgrade' print >>sys.stderr, 'fatal: %s' % e result = 128 diff --git a/manifest_xml.py b/manifest_xml.py index 04cabaad..ab842f3f 100644 --- a/manifest_xml.py +++ b/manifest_xml.py @@ -336,7 +336,7 @@ class XmlManifest(object): # tricky. actual parsing implementation may vary. except (KeyboardInterrupt, RuntimeError, SystemExit): raise - except Exception, e: + except Exception as e: raise ManifestParseError( "failed parsing included manifest %s: %s", (name, e)) else: diff --git a/project.py b/project.py index 472b1d32..96feb94b 100644 --- a/project.py +++ b/project.py @@ -1044,7 +1044,7 @@ class Project(object): try: self._Checkout(revid, quiet=True) - except GitError, e: + except GitError as e: syncbuf.fail(self, e) return self._CopyFiles() @@ -1066,7 +1066,7 @@ class Project(object): branch.name) try: self._Checkout(revid, quiet=True) - except GitError, e: + except GitError as e: syncbuf.fail(self, e) return self._CopyFiles() @@ -1151,7 +1151,7 @@ class Project(object): try: self._ResetHard(revid) self._CopyFiles() - except GitError, e: + except GitError as e: syncbuf.fail(self, e) return else: @@ -1723,7 +1723,7 @@ class Project(object): continue try: os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst) - except OSError, e: + except OSError as e: if e.errno == errno.EPERM: raise GitError('filesystem must support symlinks') else: @@ -1786,7 +1786,7 @@ class Project(object): os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst) else: raise GitError('cannot overwrite a local work tree') - except OSError, e: + except OSError as e: if e.errno == errno.EPERM: raise GitError('filesystem must support symlinks') else: diff --git a/repo b/repo index 32cd1782..7942851b 100755 --- a/repo +++ b/repo @@ -185,7 +185,7 @@ def _Init(args): if not os.path.isdir(repodir): try: os.mkdir(repodir) - except OSError, e: + except OSError as e: print >>sys.stderr, \ 'fatal: cannot make %s directory: %s' % ( repodir, e.strerror) @@ -221,7 +221,7 @@ def _CheckGitVersion(): cmd = [GIT, '--version'] try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) - except OSError, e: + except OSError as e: print >>sys.stderr print >>sys.stderr, "fatal: '%s' is not available" % GIT print >>sys.stderr, 'fatal: %s' % e @@ -268,7 +268,7 @@ def _SetupGnuPG(quiet): if not os.path.isdir(home_dot_repo): try: os.mkdir(home_dot_repo) - except OSError, e: + except OSError as e: print >>sys.stderr, \ 'fatal: cannot make %s directory: %s' % ( home_dot_repo, e.strerror) @@ -277,7 +277,7 @@ def _SetupGnuPG(quiet): if not os.path.isdir(gpg_dir): try: os.mkdir(gpg_dir, 0700) - except OSError, e: + except OSError as e: print >>sys.stderr, \ 'fatal: cannot make %s directory: %s' % ( gpg_dir, e.strerror) @@ -291,7 +291,7 @@ def _SetupGnuPG(quiet): proc = subprocess.Popen(cmd, env = env, stdin = subprocess.PIPE) - except OSError, e: + except OSError as e: if not quiet: print >>sys.stderr, 'warning: gpg (GnuPG) is not available.' print >>sys.stderr, 'warning: Installing it is strongly encouraged.' @@ -392,13 +392,13 @@ def _DownloadBundle(url, local, quiet): try: try: r = urllib2.urlopen(url) - except urllib2.HTTPError, e: + except urllib2.HTTPError as e: if e.code == 404: return False print >>sys.stderr, 'fatal: Cannot get %s' % url print >>sys.stderr, 'fatal: HTTP error %s' % e.code raise CloneFailure() - except urllib2.URLError, e: + except urllib2.URLError as e: print >>sys.stderr, 'fatal: Cannot get %s' % url print >>sys.stderr, 'fatal: error %s' % e.reason raise CloneFailure() @@ -427,7 +427,7 @@ def _Clone(url, local, quiet): """ try: os.mkdir(local) - except OSError, e: + except OSError as e: print >>sys.stderr, \ 'fatal: cannot make %s directory: %s' \ % (local, e.strerror) @@ -436,7 +436,7 @@ def _Clone(url, local, quiet): cmd = [GIT, 'init', '--quiet'] try: proc = subprocess.Popen(cmd, cwd = local) - except OSError, e: + except OSError as e: print >>sys.stderr print >>sys.stderr, "fatal: '%s' is not available" % GIT print >>sys.stderr, 'fatal: %s' % e @@ -699,7 +699,7 @@ def main(orig_args): me.extend(extra_args) try: 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: %s" % e sys.exit(148) diff --git a/subcmds/init.py b/subcmds/init.py index 007667e2..b6b98076 100644 --- a/subcmds/init.py +++ b/subcmds/init.py @@ -207,7 +207,7 @@ to update the working directory files. try: self.manifest.Link(name) - except ManifestParseError, e: + except ManifestParseError as e: print >>sys.stderr, "fatal: manifest '%s' not available" % name print >>sys.stderr, 'fatal: %s' % str(e) sys.exit(1) diff --git a/subcmds/sync.py b/subcmds/sync.py index e68a025e..d16605ff 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -417,7 +417,7 @@ uncommitted changes are present' % project.relpath # in the .netrc file. print >>sys.stderr, 'No credentials found for %s in .netrc' % \ parse_result.hostname - except netrc.NetrcParseError, e: + except netrc.NetrcParseError as e: print >>sys.stderr, 'Error parsing .netrc file: %s' % e if (username and password): @@ -464,11 +464,11 @@ uncommitted changes are present' % project.relpath else: print >>sys.stderr, 'error: %s' % manifest_str 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' % ( self.manifest.manifest_server, e) 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' % ( self.manifest.manifest_server, e.errcode, e.errmsg) sys.exit(1) diff --git a/subcmds/upload.py b/subcmds/upload.py index 685e3420..84a5e440 100644 --- a/subcmds/upload.py +++ b/subcmds/upload.py @@ -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.uploaded = True - except UploadError, e: + except UploadError as e: branch.error = e branch.uploaded = False 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] try: 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) return