From 7c93846385bdb4beed700cb9e971794b71f3403c Mon Sep 17 00:00:00 2001 From: Raul Tambre Date: Fri, 24 May 2019 16:35:35 +0000 Subject: [PATCH] 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 Auto-Submit: Raul Tambre Reviewed-by: Dirk Pranke --- checkout.py | 4 ++-- clang_format_merge_driver.py | 2 +- dart_format.py | 2 +- my_activity.py | 2 +- presubmit_support.py | 8 ++++---- rietveld.py | 8 ++++---- testing_support/super_mox.py | 2 +- tests/checkout_test.py | 2 +- tests/gclient_scm_test.py | 2 +- tests/gclient_test.py | 4 ++-- tests/owners_unittest.py | 4 ++-- tests/patch_test.py | 2 +- tests/presubmit_unittest.py | 2 +- tests/scm_unittest.py | 2 +- tests/subprocess2_test.py | 12 ++++++------ tests/upload_to_google_storage_unittest.py | 2 +- watchlists.py | 4 ++-- 17 files changed, 32 insertions(+), 32 deletions(-) diff --git a/checkout.py b/checkout.py index 7e91afcb9..9155446b1 100644 --- a/checkout.py +++ b/checkout.py @@ -292,9 +292,9 @@ class GitCheckout(CheckoutBase): if verbose: print(p.filename) print(align_stdout(stdout)) - except OSError, e: + except OSError as e: errors.append((p, '%s%s' % (align_stdout(stdout), e))) - except subprocess.CalledProcessError, e: + except subprocess.CalledProcessError as e: errors.append((p, 'While running %s;\n%s%s' % ( ' '.join(e.cmd), diff --git a/clang_format_merge_driver.py b/clang_format_merge_driver.py index 450a91617..86e803372 100755 --- a/clang_format_merge_driver.py +++ b/clang_format_merge_driver.py @@ -57,7 +57,7 @@ def main(): stdin=input_file) with open(fpath, 'wb') as output_file: output_file.write(output) - except clang_format.NotFoundError, e: + except clang_format.NotFoundError as e: print(e) print('Failed to find clang-format. Falling-back on standard 3-way merge') diff --git a/dart_format.py b/dart_format.py index 5fec2a3d9..f374a952a 100755 --- a/dart_format.py +++ b/dart_format.py @@ -43,7 +43,7 @@ def FindDartFmtToolInChromiumTree(): def main(args): try: tool = FindDartFmtToolInChromiumTree() - except NotFoundError, e: + except NotFoundError as e: print(e, file=sys.stderr) sys.exit(1) diff --git a/my_activity.py b/my_activity.py index 581e7d91c..33db18bb6 100755 --- a/my_activity.py +++ b/my_activity.py @@ -377,7 +377,7 @@ class MyActivity(object): return list(gerrit_util.GenerateAllChanges(instance['url'], req, o_params=['MESSAGES', 'LABELS', 'DETAILED_ACCOUNTS', 'CURRENT_REVISION', 'CURRENT_COMMIT'])) - except gerrit_util.GerritError, e: + except gerrit_util.GerritError as e: error_message = 'Looking up %r: %s' % (instance['url'], e) if error_message not in self.access_errors: self.access_errors.add(error_message) diff --git a/presubmit_support.py b/presubmit_support.py index 012539407..dd3bc4937 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -1220,7 +1220,7 @@ class GetTryMastersExecuter(object): try: exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True), context) - except Exception, e: + except Exception as e: raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) @@ -1252,7 +1252,7 @@ class GetPostUploadExecuter(object): try: exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True), context) - except Exception, e: + except Exception as e: raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) @@ -1418,7 +1418,7 @@ class PresubmitExecuter(object): try: exec(compile(script_text, 'PRESUBMIT.py', 'exec', dont_inherit=True), context) - except Exception, e: + except Exception as e: raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) # These function names must change if we make substantial changes to @@ -1719,7 +1719,7 @@ def main(argv=None): options.dry_run, options.parallel) return not results.should_continue() - except PresubmitFailure, e: + except PresubmitFailure as e: print(e, file=sys.stderr) print('Maybe your depot_tools is out of date?', file=sys.stderr) return 2 diff --git a/rietveld.py b/rietveld.py index 87d4eb2a6..450f8c995 100644 --- a/rietveld.py +++ b/rietveld.py @@ -167,7 +167,7 @@ class Rietveld(object): try: diff = self.get_file_diff(issue, patchset, state['id']) - except urllib2.HTTPError, e: + except urllib2.HTTPError as e: if e.code == 404: raise patch.UnsupportedPatchFormat( filename, 'File doesn\'t have a diff.') @@ -433,7 +433,7 @@ class Rietveld(object): try: logging.debug('%s' % request_path) return self.rpc_server.Send(request_path, **kwargs) - except urllib2.HTTPError, e: + except urllib2.HTTPError as e: if retry >= (self._maxtries - 1): raise flake_codes = {500, 502, 503} @@ -441,7 +441,7 @@ class Rietveld(object): flake_codes.add(404) if e.code not in flake_codes: raise - except urllib2.URLError, e: + except urllib2.URLError as e: if retry >= (self._maxtries - 1): raise @@ -468,7 +468,7 @@ class Rietveld(object): logging.error('Caught urllib2.URLError %s which wasn\'t deemed ' 'transient', e.reason) raise - except socket.error, e: + except socket.error as e: if retry >= (self._maxtries - 1): raise if not 'timed out' in str(e): diff --git a/testing_support/super_mox.py b/testing_support/super_mox.py index b71d43496..9455a4cf1 100644 --- a/testing_support/super_mox.py +++ b/testing_support/super_mox.py @@ -140,7 +140,7 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): if hasattr(parent, item): try: self.mox.StubOutWithMock(parent, item) - except TypeError, e: + except TypeError as e: raise TypeError( 'Couldn\'t mock %s in %s: %s' % (item, parent.__name__, e)) diff --git a/tests/checkout_test.py b/tests/checkout_test.py index ff2208723..5aec9899c 100755 --- a/tests/checkout_test.py +++ b/tests/checkout_test.py @@ -131,7 +131,7 @@ class BaseTest(fake_repos.FakeReposTestBase): try: co.apply_patch([patch.FilePatchDiff('chrome/file.cc', BAD_PATCH, [])]) self.fail() - except checkout.PatchApplicationFailed, e: + except checkout.PatchApplicationFailed as e: self.assertEquals(e.filename, 'chrome/file.cc') self.assertEquals(e.status, err_msg) diff --git a/tests/gclient_scm_test.py b/tests/gclient_scm_test.py index d2305ff3d..f1b712e78 100755 --- a/tests/gclient_scm_test.py +++ b/tests/gclient_scm_test.py @@ -50,7 +50,7 @@ class GCBaseTestCase(object): """Like unittest's assertRaises() but checks for Gclient.Error.""" try: fn(*args, **kwargs) - except gclient_scm.gclient_utils.Error, e: + except gclient_scm.gclient_utils.Error as e: self.assertEquals(e.args[0], msg) else: self.fail('%s not raised' % msg) diff --git a/tests/gclient_test.py b/tests/gclient_test.py index 710273fdc..2f592e2d3 100755 --- a/tests/gclient_test.py +++ b/tests/gclient_test.py @@ -1105,7 +1105,7 @@ class GclientTest(trial_dir.TestCase): try: obj.RunOnDeps('None', []) self.fail() - except gclient_utils.Error, e: + except gclient_utils.Error as e: self.assertIn('allowed_hosts must be', str(e)) finally: self._get_processed() @@ -1130,7 +1130,7 @@ class GclientTest(trial_dir.TestCase): try: obj.RunOnDeps('None', []) self.fail() - except gclient_utils.Error, e: + except gclient_utils.Error as e: self.assertIn('allowed_hosts must be', str(e)) finally: self._get_processed() diff --git a/tests/owners_unittest.py b/tests/owners_unittest.py index b416396e3..2b308da70 100755 --- a/tests/owners_unittest.py +++ b/tests/owners_unittest.py @@ -318,7 +318,7 @@ class OwnersDatabaseTest(_BaseTestCase): try: self.db().reviewers_for(['ipc/ipc_message_utils.h'], None) self.fail() # pragma: no cover - except owners.SyntaxErrorInOwnersFile, e: + except owners.SyntaxErrorInOwnersFile as e: self.assertTrue(str(e).startswith('/ipc/OWNERS:1')) def assert_syntax_error(self, owners_file_contents): @@ -328,7 +328,7 @@ class OwnersDatabaseTest(_BaseTestCase): try: db.reviewers_for(['foo/DEPS'], None) self.fail() # pragma: no cover - except owners.SyntaxErrorInOwnersFile, e: + except owners.SyntaxErrorInOwnersFile as e: self.assertTrue(str(e).startswith('/foo/OWNERS:1')) def test_syntax_error__unknown_token(self): diff --git a/tests/patch_test.py b/tests/patch_test.py index e932abb29..2414a90ce 100755 --- a/tests/patch_test.py +++ b/tests/patch_test.py @@ -416,7 +416,7 @@ class PatchTestFail(unittest.TestCase): try: patch.FilePatchDiff('foo', RAW.PATCH, []) self.fail() - except patch.UnsupportedPatchFormat, e: + except patch.UnsupportedPatchFormat as e: self.assertEquals( "Can't process patch for file foo.\nUnexpected diff: chrome/file.cc.", str(e)) diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index c06c5d88b..712ee80eb 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -920,7 +920,7 @@ def CheckChangeOnCommit(input_api, output_api): try: presubmit.main(['--root', self.fake_root_dir]) self.fail() - except SystemExit, e: + except SystemExit as e: self.assertEquals(2, e.code) diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index 2cb86c0b1..0cdede39a 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -28,7 +28,7 @@ class BaseTestCase(SuperMoxTestBase): def assertRaisesError(self, msg, fn, *args, **kwargs): try: fn(*args, **kwargs) - except scm.gclient_utils.Error, e: + except scm.gclient_utils.Error as e: self.assertEquals(e.args[0], msg) else: self.fail('%s not raised' % msg) diff --git a/tests/subprocess2_test.py b/tests/subprocess2_test.py index 4aac67c37..1e3187bb8 100755 --- a/tests/subprocess2_test.py +++ b/tests/subprocess2_test.py @@ -254,7 +254,7 @@ class RegressionTest(BaseTestCase): subp.check_output( e + ['--fail', '--stdout'], universal_newlines=un) self.fail() - except subp.CalledProcessError, exception: + except subp.CalledProcessError as exception: self._check_exception(subp, exception, c('A\nBB\nCCC\n'), None, 64) self._run_test(fn) @@ -266,7 +266,7 @@ class RegressionTest(BaseTestCase): subp.check_output( e + ['--fail', '--stderr'], universal_newlines=un) self.fail() - except subp.CalledProcessError, exception: + except subp.CalledProcessError as exception: self._check_exception(subp, exception, c(''), None, 64) self._run_test(fn) @@ -280,7 +280,7 @@ class RegressionTest(BaseTestCase): stderr=subp.PIPE, universal_newlines=un) self.fail() - except subp.CalledProcessError, exception: + except subp.CalledProcessError as exception: self._check_exception(subp, exception, '', c('a\nbb\nccc\n'), 64) self._run_test(fn) @@ -294,7 +294,7 @@ class RegressionTest(BaseTestCase): stderr=subp.STDOUT, universal_newlines=un) self.fail() - except subp.CalledProcessError, exception: + except subp.CalledProcessError as exception: self._check_exception(subp, exception, c('a\nbb\nccc\n'), None, 64) self._run_test(fn) @@ -303,7 +303,7 @@ class RegressionTest(BaseTestCase): try: subp.check_call(self.exe + ['--fail', '--stderr']) self.fail() - except subp.CalledProcessError, exception: + except subp.CalledProcessError as exception: self._check_exception(subp, exception, None, None, 64) def test_redirect_stderr_to_stdout_pipe(self): @@ -515,7 +515,7 @@ class S2Test(BaseTestCase): stderr=stderr.append, universal_newlines=un) self.fail() - except subprocess2.CalledProcessError, exception: + except subprocess2.CalledProcessError as exception: self._check_exception(exception, '', None, 64) self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) self._run_test(fn) diff --git a/tests/upload_to_google_storage_unittest.py b/tests/upload_to_google_storage_unittest.py index 93c1fddf3..a6b698a05 100755 --- a/tests/upload_to_google_storage_unittest.py +++ b/tests/upload_to_google_storage_unittest.py @@ -159,7 +159,7 @@ class UploadTests(unittest.TestCase): try: upload_to_google_storage.get_targets([], self.parser, False) self.fail() - except SystemExit, e: + except SystemExit as e: self.assertEqual(e.code, 2) def test_get_targets_passthrough(self): diff --git a/watchlists.py b/watchlists.py index 71c453e85..096eb10e7 100755 --- a/watchlists.py +++ b/watchlists.py @@ -62,7 +62,7 @@ class Watchlists(object): contents = watchlists_file.read() watchlists_file.close() return contents - except IOError, e: + except IOError as e: logging.error("Cannot read %s: %s" % (self._GetRulesFilePath(), e)) return '' @@ -75,7 +75,7 @@ class Watchlists(object): watchlists_data = None try: watchlists_data = eval(contents, {'__builtins__': None}, None) - except SyntaxError, e: + except SyntaxError as e: logging.error("Cannot parse %s. %s" % (self._GetRulesFilePath(), e)) return