Python 3 - modernize exception catching syntax.

Old-style syntax:  `except Exception, e:`
New-style syntax:  `except Exception as e:`

These two statements are identical, except that the former has
been deprecated for may versions, and was removed in Python 3.

This converts everything to use the new syntax (which also works
in Python 2).  I had to convert unittest2 as well.  What we really
need to do is just delete unittest2, and use unittest instead since
it is a standard module that ships with every Python distribution.
But this is the path of least resistance for now, although at
some point we will really need to do it.

llvm-svn: 251968
This commit is contained in:
Zachary Turner 2015-11-03 19:49:05 +00:00
parent 56e0aafa2b
commit c07138f6a0
10 changed files with 40 additions and 40 deletions

View File

@ -52,7 +52,7 @@ class libcxx_hash_table_SynthProvider:
self.next_element = self.begin_ptr
else:
self.next_element = None
except Exception, e:
except Exception as e:
logger >> "Caught exception: %r" % e
pass

View File

@ -348,7 +348,7 @@ class TestCase(unittest.TestCase):
success = False
try:
self.setUp()
except SkipTest, e:
except SkipTest as e:
self._addSkip(result, str(e))
except Exception:
result.addError(self, sys.exc_info())
@ -386,7 +386,7 @@ class TestCase(unittest.TestCase):
testMethod()
except self.failureException:
result.addFailure(self, sys.exc_info())
except _ExpectedFailure, e:
except _ExpectedFailure as e:
addExpectedFailure = getattr(result, 'addExpectedFailure', None)
if addExpectedFailure is not None:
addExpectedFailure(self, e.exc_info, e.bugnumber)
@ -394,7 +394,7 @@ class TestCase(unittest.TestCase):
warnings.warn("Use of a TestResult without an addExpectedFailure method is deprecated",
DeprecationWarning)
result.addSuccess(self)
except _UnexpectedSuccess, x:
except _UnexpectedSuccess as x:
addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)
if addUnexpectedSuccess is not None:
addUnexpectedSuccess(self, x.bugnumber)
@ -402,7 +402,7 @@ class TestCase(unittest.TestCase):
warnings.warn("Use of a TestResult without an addUnexpectedSuccess method is deprecated",
DeprecationWarning)
result.addFailure(self, sys.exc_info())
except SkipTest, e:
except SkipTest as e:
self._addSkip(result, str(e))
except Exception:
result.addError(self, sys.exc_info())
@ -807,16 +807,16 @@ class TestCase(unittest.TestCase):
"""
try:
difference1 = set1.difference(set2)
except TypeError, e:
except TypeError as e:
self.fail('invalid type when attempting set difference: %s' % e)
except AttributeError, e:
except AttributeError as e:
self.fail('first argument does not support set difference: %s' % e)
try:
difference2 = set2.difference(set1)
except TypeError, e:
except TypeError as e:
self.fail('invalid type when attempting set difference: %s' % e)
except AttributeError, e:
except AttributeError as e:
self.fail('second argument does not support set difference: %s' % e)
if not (difference1 or difference2):
@ -1017,7 +1017,7 @@ class TestCase(unittest.TestCase):
return _AssertRaisesContext(expected_exception, self, expected_regexp)
try:
callable_obj(*args, **kwargs)
except expected_exception, exc_value:
except expected_exception as exc_value:
if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(str(exc_value)):

View File

@ -89,7 +89,7 @@ class TestLoader(unittest.TestLoader):
if use_load_tests and load_tests is not None:
try:
return load_tests(self, tests, None)
except Exception, e:
except Exception as e:
return _make_failed_load_tests(module.__name__, e,
self.suiteClass)
return tests
@ -295,7 +295,7 @@ class TestLoader(unittest.TestLoader):
else:
try:
yield load_tests(self, tests, pattern)
except Exception, e:
except Exception as e:
yield _make_failed_load_tests(package.__name__, e,
self.suiteClass)

View File

@ -150,7 +150,7 @@ class TestProgram(object):
else:
self.testNames = (self.defaultTest,)
self.createTests()
except getopt.error, msg:
except getopt.error as msg:
self.usageExit(msg)
def createTests(self):

View File

@ -138,7 +138,7 @@ class TestSuite(BaseTestSuite):
if setUpClass is not None:
try:
setUpClass()
except Exception, e:
except Exception as e:
if isinstance(result, _DebugResult):
raise
currentClass._classSetupFailed = True
@ -172,7 +172,7 @@ class TestSuite(BaseTestSuite):
if setUpModule is not None:
try:
setUpModule()
except Exception, e:
except Exception as e:
if isinstance(result, _DebugResult):
raise
result._moduleSetUpFailed = True
@ -203,7 +203,7 @@ class TestSuite(BaseTestSuite):
if tearDownModule is not None:
try:
tearDownModule()
except Exception, e:
except Exception as e:
if isinstance(result, _DebugResult):
raise
errorName = 'tearDownModule (%s)' % previousModule
@ -225,7 +225,7 @@ class TestSuite(BaseTestSuite):
if tearDownClass is not None:
try:
tearDownClass()
except Exception, e:
except Exception as e:
if isinstance(result, _DebugResult):
raise
className = util.strclass(previousClass)

View File

@ -109,7 +109,7 @@ class HashingMixin(object):
self.fail("%r and %r do not hash equal" % (obj_1, obj_2))
except KeyboardInterrupt:
raise
except Exception, e:
except Exception as e:
self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e))
for obj_1, obj_2 in self.ne_pairs:
@ -119,7 +119,7 @@ class HashingMixin(object):
(obj_1, obj_2))
except KeyboardInterrupt:
raise
except Exception, e:
except Exception as e:
self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))

View File

@ -62,7 +62,7 @@ class Test_Assertions(unittest2.TestCase):
self.assertNotRegexpMatches('Ala ma kota', r'r+')
try:
self.assertNotRegexpMatches('Ala ma kota', r'k.t', 'Message')
except self.failureException, e:
except self.failureException as e:
self.assertIn("'kot'", e.args[0])
self.assertIn('Message', e.args[0])
else:

View File

@ -893,7 +893,7 @@ test case
try:
self.assertMultiLineEqual(type_changer(sample_text),
type_changer(revised_sample_text))
except self.failureException, e:
except self.failureException as e:
# need to remove the first line of the error message
error = str(e).encode('utf8').split('\n', 1)[1]
@ -913,7 +913,7 @@ test case
self.maxDiff = len(diff)//2
try:
self.assertSequenceEqual(seq1, seq2)
except self.failureException, e:
except self.failureException as e:
msg = e.args[0]
else:
self.fail('assertSequenceEqual did not fail.')
@ -923,7 +923,7 @@ test case
self.maxDiff = len(diff) * 2
try:
self.assertSequenceEqual(seq1, seq2)
except self.failureException, e:
except self.failureException as e:
msg = e.args[0]
else:
self.fail('assertSequenceEqual did not fail.')
@ -933,7 +933,7 @@ test case
self.maxDiff = None
try:
self.assertSequenceEqual(seq1, seq2)
except self.failureException, e:
except self.failureException as e:
msg = e.args[0]
else:
self.fail('assertSequenceEqual did not fail.')
@ -961,7 +961,7 @@ test case
test._truncateMessage = truncate
try:
test.assertDictEqual({}, {1: 0})
except self.failureException, e:
except self.failureException as e:
self.assertEqual(str(e), 'foo')
else:
self.fail('assertDictEqual did not fail')
@ -973,7 +973,7 @@ test case
test._truncateMessage = truncate
try:
test.assertMultiLineEqual('foo', 'bar')
except self.failureException, e:
except self.failureException as e:
self.assertEqual(str(e), 'foo')
else:
self.fail('assertMultiLineEqual did not fail')

View File

@ -205,7 +205,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromName('')
except ValueError, e:
except ValueError as e:
self.assertEqual(str(e), "Empty module name")
else:
self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
@ -238,7 +238,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromName('sdasfasfasdf')
except ImportError, e:
except ImportError as e:
self.assertEqual(str(e), "No module named sdasfasfasdf")
else:
self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
@ -254,7 +254,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromName('unittest2.sdasfasfasdf')
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
else:
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
@ -271,7 +271,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromName('sdasfasfasdf', unittest2)
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
else:
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
@ -425,7 +425,7 @@ class Test_TestLoader(unittest2.TestCase):
loader = unittest2.TestLoader()
try:
loader.loadTestsFromName('testcase_1.testfoo', m)
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
else:
self.fail("Failed to raise AttributeError")
@ -583,7 +583,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromNames([''])
except ValueError, e:
except ValueError as e:
self.assertEqual(str(e), "Empty module name")
else:
self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
@ -618,7 +618,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromNames(['sdasfasfasdf'])
except ImportError, e:
except ImportError as e:
self.assertEqual(str(e), "No module named sdasfasfasdf")
else:
self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
@ -634,7 +634,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromNames(['unittest2.sdasfasfasdf', 'unittest2'])
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
else:
self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
@ -653,7 +653,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromNames(['sdasfasfasdf'], unittest2)
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
else:
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
@ -672,7 +672,7 @@ class Test_TestLoader(unittest2.TestCase):
try:
loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest2)
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
else:
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
@ -821,7 +821,7 @@ class Test_TestLoader(unittest2.TestCase):
loader = unittest2.TestLoader()
try:
loader.loadTestsFromNames(['testcase_1.testfoo'], m)
except AttributeError, e:
except AttributeError as e:
self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
else:
self.fail("Failed to raise AttributeError")

View File

@ -35,7 +35,7 @@ class TestWith(unittest2.TestCase):
self.assertRaises(KeyError, _raise, KeyError("key"))
try:
self.assertRaises(KeyError, lambda: None)
except self.failureException, e:
except self.failureException as e:
self.assertIn("KeyError not raised", e.args)
else:
self.fail("assertRaises() didn't fail")
@ -48,7 +48,7 @@ class TestWith(unittest2.TestCase):
with self.assertRaises(KeyError) as cm:
try:
raise KeyError
except Exception, e:
except Exception as e:
raise
self.assertIs(cm.exception, e)
@ -57,7 +57,7 @@ class TestWith(unittest2.TestCase):
try:
with self.assertRaises(KeyError):
pass
except self.failureException, e:
except self.failureException as e:
self.assertIn("KeyError not raised", e.args)
else:
self.fail("assertRaises() didn't fail")