Commit 7c938463 authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

Convert except statements to be Python 3 compatible

Ran "2to3 -w -n -f except ./".

The scripts still work with Python 2.
There are no intended behaviour changes.

Bug: 942522
Change-Id: Ifa274cb83f74cfa8ce092fffbb88f3ab5309e72c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1607841
Commit-Queue: Raul Tambre <raul@tambre.ee>
Auto-Submit: Raul Tambre <raul@tambre.ee>
Reviewed-by: 's avatarDirk Pranke <dpranke@chromium.org>
parent 7d1300b8
...@@ -292,9 +292,9 @@ class GitCheckout(CheckoutBase): ...@@ -292,9 +292,9 @@ class GitCheckout(CheckoutBase):
if verbose: if verbose:
print(p.filename) print(p.filename)
print(align_stdout(stdout)) print(align_stdout(stdout))
except OSError, e: except OSError as e:
errors.append((p, '%s%s' % (align_stdout(stdout), e))) errors.append((p, '%s%s' % (align_stdout(stdout), e)))
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError as e:
errors.append((p, errors.append((p,
'While running %s;\n%s%s' % ( 'While running %s;\n%s%s' % (
' '.join(e.cmd), ' '.join(e.cmd),
......
...@@ -57,7 +57,7 @@ def main(): ...@@ -57,7 +57,7 @@ def main():
stdin=input_file) stdin=input_file)
with open(fpath, 'wb') as output_file: with open(fpath, 'wb') as output_file:
output_file.write(output) output_file.write(output)
except clang_format.NotFoundError, e: except clang_format.NotFoundError as e:
print(e) print(e)
print('Failed to find clang-format. Falling-back on standard 3-way merge') print('Failed to find clang-format. Falling-back on standard 3-way merge')
......
...@@ -43,7 +43,7 @@ def FindDartFmtToolInChromiumTree(): ...@@ -43,7 +43,7 @@ def FindDartFmtToolInChromiumTree():
def main(args): def main(args):
try: try:
tool = FindDartFmtToolInChromiumTree() tool = FindDartFmtToolInChromiumTree()
except NotFoundError, e: except NotFoundError as e:
print(e, file=sys.stderr) print(e, file=sys.stderr)
sys.exit(1) sys.exit(1)
......
...@@ -377,7 +377,7 @@ class MyActivity(object): ...@@ -377,7 +377,7 @@ class MyActivity(object):
return list(gerrit_util.GenerateAllChanges(instance['url'], req, return list(gerrit_util.GenerateAllChanges(instance['url'], req,
o_params=['MESSAGES', 'LABELS', 'DETAILED_ACCOUNTS', o_params=['MESSAGES', 'LABELS', 'DETAILED_ACCOUNTS',
'CURRENT_REVISION', 'CURRENT_COMMIT'])) 'CURRENT_REVISION', 'CURRENT_COMMIT']))
except gerrit_util.GerritError, e: except gerrit_util.GerritError as e:
error_message = 'Looking up %r: %s' % (instance['url'], e) error_message = 'Looking up %r: %s' % (instance['url'], e)
if error_message not in self.access_errors: if error_message not in self.access_errors:
self.access_errors.add(error_message) self.access_errors.add(error_message)
......
...@@ -1220,7 +1220,7 @@ class GetTryMastersExecuter(object): ...@@ -1220,7 +1220,7 @@ class GetTryMastersExecuter(object):
try: try:
exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True), exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True),
context) context)
except Exception, e: except Exception as e:
raise PresubmitFailure('"%s" had an exception.\n%s' raise PresubmitFailure('"%s" had an exception.\n%s'
% (presubmit_path, e)) % (presubmit_path, e))
...@@ -1252,7 +1252,7 @@ class GetPostUploadExecuter(object): ...@@ -1252,7 +1252,7 @@ class GetPostUploadExecuter(object):
try: try:
exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True), exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True),
context) context)
except Exception, e: except Exception as e:
raise PresubmitFailure('"%s" had an exception.\n%s' raise PresubmitFailure('"%s" had an exception.\n%s'
% (presubmit_path, e)) % (presubmit_path, e))
...@@ -1418,7 +1418,7 @@ class PresubmitExecuter(object): ...@@ -1418,7 +1418,7 @@ class PresubmitExecuter(object):
try: try:
exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True), exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True),
context) context)
except Exception, e: except Exception as e:
raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e))
# These function names must change if we make substantial changes to # These function names must change if we make substantial changes to
...@@ -1719,7 +1719,7 @@ def main(argv=None): ...@@ -1719,7 +1719,7 @@ def main(argv=None):
options.dry_run, options.dry_run,
options.parallel) options.parallel)
return not results.should_continue() return not results.should_continue()
except PresubmitFailure, e: except PresubmitFailure as e:
print(e, file=sys.stderr) print(e, file=sys.stderr)
print('Maybe your depot_tools is out of date?', file=sys.stderr) print('Maybe your depot_tools is out of date?', file=sys.stderr)
return 2 return 2
......
...@@ -167,7 +167,7 @@ class Rietveld(object): ...@@ -167,7 +167,7 @@ class Rietveld(object):
try: try:
diff = self.get_file_diff(issue, patchset, state['id']) diff = self.get_file_diff(issue, patchset, state['id'])
except urllib2.HTTPError, e: except urllib2.HTTPError as e:
if e.code == 404: if e.code == 404:
raise patch.UnsupportedPatchFormat( raise patch.UnsupportedPatchFormat(
filename, 'File doesn\'t have a diff.') filename, 'File doesn\'t have a diff.')
...@@ -433,7 +433,7 @@ class Rietveld(object): ...@@ -433,7 +433,7 @@ class Rietveld(object):
try: try:
logging.debug('%s' % request_path) logging.debug('%s' % request_path)
return self.rpc_server.Send(request_path, **kwargs) return self.rpc_server.Send(request_path, **kwargs)
except urllib2.HTTPError, e: except urllib2.HTTPError as e:
if retry >= (self._maxtries - 1): if retry >= (self._maxtries - 1):
raise raise
flake_codes = {500, 502, 503} flake_codes = {500, 502, 503}
...@@ -441,7 +441,7 @@ class Rietveld(object): ...@@ -441,7 +441,7 @@ class Rietveld(object):
flake_codes.add(404) flake_codes.add(404)
if e.code not in flake_codes: if e.code not in flake_codes:
raise raise
except urllib2.URLError, e: except urllib2.URLError as e:
if retry >= (self._maxtries - 1): if retry >= (self._maxtries - 1):
raise raise
...@@ -468,7 +468,7 @@ class Rietveld(object): ...@@ -468,7 +468,7 @@ class Rietveld(object):
logging.error('Caught urllib2.URLError %s which wasn\'t deemed ' logging.error('Caught urllib2.URLError %s which wasn\'t deemed '
'transient', e.reason) 'transient', e.reason)
raise raise
except socket.error, e: except socket.error as e:
if retry >= (self._maxtries - 1): if retry >= (self._maxtries - 1):
raise raise
if not 'timed out' in str(e): if not 'timed out' in str(e):
......
...@@ -140,7 +140,7 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): ...@@ -140,7 +140,7 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase):
if hasattr(parent, item): if hasattr(parent, item):
try: try:
self.mox.StubOutWithMock(parent, item) self.mox.StubOutWithMock(parent, item)
except TypeError, e: except TypeError as e:
raise TypeError( raise TypeError(
'Couldn\'t mock %s in %s: %s' % (item, parent.__name__, e)) 'Couldn\'t mock %s in %s: %s' % (item, parent.__name__, e))
......
...@@ -131,7 +131,7 @@ class BaseTest(fake_repos.FakeReposTestBase): ...@@ -131,7 +131,7 @@ class BaseTest(fake_repos.FakeReposTestBase):
try: try:
co.apply_patch([patch.FilePatchDiff('chrome/file.cc', BAD_PATCH, [])]) co.apply_patch([patch.FilePatchDiff('chrome/file.cc', BAD_PATCH, [])])
self.fail() self.fail()
except checkout.PatchApplicationFailed, e: except checkout.PatchApplicationFailed as e:
self.assertEquals(e.filename, 'chrome/file.cc') self.assertEquals(e.filename, 'chrome/file.cc')
self.assertEquals(e.status, err_msg) self.assertEquals(e.status, err_msg)
......
...@@ -50,7 +50,7 @@ class GCBaseTestCase(object): ...@@ -50,7 +50,7 @@ class GCBaseTestCase(object):
"""Like unittest's assertRaises() but checks for Gclient.Error.""" """Like unittest's assertRaises() but checks for Gclient.Error."""
try: try:
fn(*args, **kwargs) fn(*args, **kwargs)
except gclient_scm.gclient_utils.Error, e: except gclient_scm.gclient_utils.Error as e:
self.assertEquals(e.args[0], msg) self.assertEquals(e.args[0], msg)
else: else:
self.fail('%s not raised' % msg) self.fail('%s not raised' % msg)
......
...@@ -1105,7 +1105,7 @@ class GclientTest(trial_dir.TestCase): ...@@ -1105,7 +1105,7 @@ class GclientTest(trial_dir.TestCase):
try: try:
obj.RunOnDeps('None', []) obj.RunOnDeps('None', [])
self.fail() self.fail()
except gclient_utils.Error, e: except gclient_utils.Error as e:
self.assertIn('allowed_hosts must be', str(e)) self.assertIn('allowed_hosts must be', str(e))
finally: finally:
self._get_processed() self._get_processed()
...@@ -1130,7 +1130,7 @@ class GclientTest(trial_dir.TestCase): ...@@ -1130,7 +1130,7 @@ class GclientTest(trial_dir.TestCase):
try: try:
obj.RunOnDeps('None', []) obj.RunOnDeps('None', [])
self.fail() self.fail()
except gclient_utils.Error, e: except gclient_utils.Error as e:
self.assertIn('allowed_hosts must be', str(e)) self.assertIn('allowed_hosts must be', str(e))
finally: finally:
self._get_processed() self._get_processed()
......
...@@ -318,7 +318,7 @@ class OwnersDatabaseTest(_BaseTestCase): ...@@ -318,7 +318,7 @@ class OwnersDatabaseTest(_BaseTestCase):
try: try:
self.db().reviewers_for(['ipc/ipc_message_utils.h'], None) self.db().reviewers_for(['ipc/ipc_message_utils.h'], None)
self.fail() # pragma: no cover self.fail() # pragma: no cover
except owners.SyntaxErrorInOwnersFile, e: except owners.SyntaxErrorInOwnersFile as e:
self.assertTrue(str(e).startswith('/ipc/OWNERS:1')) self.assertTrue(str(e).startswith('/ipc/OWNERS:1'))
def assert_syntax_error(self, owners_file_contents): def assert_syntax_error(self, owners_file_contents):
...@@ -328,7 +328,7 @@ class OwnersDatabaseTest(_BaseTestCase): ...@@ -328,7 +328,7 @@ class OwnersDatabaseTest(_BaseTestCase):
try: try:
db.reviewers_for(['foo/DEPS'], None) db.reviewers_for(['foo/DEPS'], None)
self.fail() # pragma: no cover self.fail() # pragma: no cover
except owners.SyntaxErrorInOwnersFile, e: except owners.SyntaxErrorInOwnersFile as e:
self.assertTrue(str(e).startswith('/foo/OWNERS:1')) self.assertTrue(str(e).startswith('/foo/OWNERS:1'))
def test_syntax_error__unknown_token(self): def test_syntax_error__unknown_token(self):
......
...@@ -416,7 +416,7 @@ class PatchTestFail(unittest.TestCase): ...@@ -416,7 +416,7 @@ class PatchTestFail(unittest.TestCase):
try: try:
patch.FilePatchDiff('foo', RAW.PATCH, []) patch.FilePatchDiff('foo', RAW.PATCH, [])
self.fail() self.fail()
except patch.UnsupportedPatchFormat, e: except patch.UnsupportedPatchFormat as e:
self.assertEquals( self.assertEquals(
"Can't process patch for file foo.\nUnexpected diff: chrome/file.cc.", "Can't process patch for file foo.\nUnexpected diff: chrome/file.cc.",
str(e)) str(e))
......
...@@ -920,7 +920,7 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -920,7 +920,7 @@ def CheckChangeOnCommit(input_api, output_api):
try: try:
presubmit.main(['--root', self.fake_root_dir]) presubmit.main(['--root', self.fake_root_dir])
self.fail() self.fail()
except SystemExit, e: except SystemExit as e:
self.assertEquals(2, e.code) self.assertEquals(2, e.code)
......
...@@ -28,7 +28,7 @@ class BaseTestCase(SuperMoxTestBase): ...@@ -28,7 +28,7 @@ class BaseTestCase(SuperMoxTestBase):
def assertRaisesError(self, msg, fn, *args, **kwargs): def assertRaisesError(self, msg, fn, *args, **kwargs):
try: try:
fn(*args, **kwargs) fn(*args, **kwargs)
except scm.gclient_utils.Error, e: except scm.gclient_utils.Error as e:
self.assertEquals(e.args[0], msg) self.assertEquals(e.args[0], msg)
else: else:
self.fail('%s not raised' % msg) self.fail('%s not raised' % msg)
......
...@@ -254,7 +254,7 @@ class RegressionTest(BaseTestCase): ...@@ -254,7 +254,7 @@ class RegressionTest(BaseTestCase):
subp.check_output( subp.check_output(
e + ['--fail', '--stdout'], universal_newlines=un) e + ['--fail', '--stdout'], universal_newlines=un)
self.fail() self.fail()
except subp.CalledProcessError, exception: except subp.CalledProcessError as exception:
self._check_exception(subp, exception, c('A\nBB\nCCC\n'), None, 64) self._check_exception(subp, exception, c('A\nBB\nCCC\n'), None, 64)
self._run_test(fn) self._run_test(fn)
...@@ -266,7 +266,7 @@ class RegressionTest(BaseTestCase): ...@@ -266,7 +266,7 @@ class RegressionTest(BaseTestCase):
subp.check_output( subp.check_output(
e + ['--fail', '--stderr'], universal_newlines=un) e + ['--fail', '--stderr'], universal_newlines=un)
self.fail() self.fail()
except subp.CalledProcessError, exception: except subp.CalledProcessError as exception:
self._check_exception(subp, exception, c(''), None, 64) self._check_exception(subp, exception, c(''), None, 64)
self._run_test(fn) self._run_test(fn)
...@@ -280,7 +280,7 @@ class RegressionTest(BaseTestCase): ...@@ -280,7 +280,7 @@ class RegressionTest(BaseTestCase):
stderr=subp.PIPE, stderr=subp.PIPE,
universal_newlines=un) universal_newlines=un)
self.fail() self.fail()
except subp.CalledProcessError, exception: except subp.CalledProcessError as exception:
self._check_exception(subp, exception, '', c('a\nbb\nccc\n'), 64) self._check_exception(subp, exception, '', c('a\nbb\nccc\n'), 64)
self._run_test(fn) self._run_test(fn)
...@@ -294,7 +294,7 @@ class RegressionTest(BaseTestCase): ...@@ -294,7 +294,7 @@ class RegressionTest(BaseTestCase):
stderr=subp.STDOUT, stderr=subp.STDOUT,
universal_newlines=un) universal_newlines=un)
self.fail() self.fail()
except subp.CalledProcessError, exception: except subp.CalledProcessError as exception:
self._check_exception(subp, exception, c('a\nbb\nccc\n'), None, 64) self._check_exception(subp, exception, c('a\nbb\nccc\n'), None, 64)
self._run_test(fn) self._run_test(fn)
...@@ -303,7 +303,7 @@ class RegressionTest(BaseTestCase): ...@@ -303,7 +303,7 @@ class RegressionTest(BaseTestCase):
try: try:
subp.check_call(self.exe + ['--fail', '--stderr']) subp.check_call(self.exe + ['--fail', '--stderr'])
self.fail() self.fail()
except subp.CalledProcessError, exception: except subp.CalledProcessError as exception:
self._check_exception(subp, exception, None, None, 64) self._check_exception(subp, exception, None, None, 64)
def test_redirect_stderr_to_stdout_pipe(self): def test_redirect_stderr_to_stdout_pipe(self):
...@@ -515,7 +515,7 @@ class S2Test(BaseTestCase): ...@@ -515,7 +515,7 @@ class S2Test(BaseTestCase):
stderr=stderr.append, stderr=stderr.append,
universal_newlines=un) universal_newlines=un)
self.fail() self.fail()
except subprocess2.CalledProcessError, exception: except subprocess2.CalledProcessError as exception:
self._check_exception(exception, '', None, 64) self._check_exception(exception, '', None, 64)
self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr))
self._run_test(fn) self._run_test(fn)
......
...@@ -159,7 +159,7 @@ class UploadTests(unittest.TestCase): ...@@ -159,7 +159,7 @@ class UploadTests(unittest.TestCase):
try: try:
upload_to_google_storage.get_targets([], self.parser, False) upload_to_google_storage.get_targets([], self.parser, False)
self.fail() self.fail()
except SystemExit, e: except SystemExit as e:
self.assertEqual(e.code, 2) self.assertEqual(e.code, 2)
def test_get_targets_passthrough(self): def test_get_targets_passthrough(self):
......
...@@ -62,7 +62,7 @@ class Watchlists(object): ...@@ -62,7 +62,7 @@ class Watchlists(object):
contents = watchlists_file.read() contents = watchlists_file.read()
watchlists_file.close() watchlists_file.close()
return contents return contents
except IOError, e: except IOError as e:
logging.error("Cannot read %s: %s" % (self._GetRulesFilePath(), e)) logging.error("Cannot read %s: %s" % (self._GetRulesFilePath(), e))
return '' return ''
...@@ -75,7 +75,7 @@ class Watchlists(object): ...@@ -75,7 +75,7 @@ class Watchlists(object):
watchlists_data = None watchlists_data = None
try: try:
watchlists_data = eval(contents, {'__builtins__': None}, None) watchlists_data = eval(contents, {'__builtins__': None}, None)
except SyntaxError, e: except SyntaxError as e:
logging.error("Cannot parse %s. %s" % (self._GetRulesFilePath(), e)) logging.error("Cannot parse %s. %s" % (self._GetRulesFilePath(), e))
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment