1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #18013 -- Use the new 'as' syntax for exceptions.

Thanks Clueless for the initial patch.
Note that unittest has been purposely left out (external package only used by Python 2.6).
This commit is contained in:
Claude Paroz
2012-04-28 18:09:37 +02:00
parent eefb00f301
commit 3904b74a3f
107 changed files with 306 additions and 354 deletions

View File

@@ -384,9 +384,9 @@ class ModelTest(TestCase):
try:
Article.objects.all()[0:1] & Article.objects.all()[4:5]
self.fail('Should raise an AssertionError')
except AssertionError, e:
except AssertionError as e:
self.assertEqual(str(e), "Cannot combine queries once a slice has been taken.")
except Exception, e:
except Exception as e:
self.fail('Should raise an AssertionError, not %s' % e)
# Negative slices are not supported, due to database constraints.
@@ -394,15 +394,15 @@ class ModelTest(TestCase):
try:
Article.objects.all()[-1]
self.fail('Should raise an AssertionError')
except AssertionError, e:
except AssertionError as e:
self.assertEqual(str(e), "Negative indexing is not supported.")
except Exception, e:
except Exception as e:
self.fail('Should raise an AssertionError, not %s' % e)
error = None
try:
Article.objects.all()[0:-5]
except Exception, e:
except Exception as e:
error = e
self.assertTrue(isinstance(error, AssertionError))
self.assertEqual(str(error), "Negative indexing is not supported.")

View File

@@ -60,7 +60,7 @@ class GetOrCreateTests(TestCase):
# the actual traceback. Refs #16340.
try:
ManualPrimaryKeyTest.objects.get_or_create(id=1, data="Different")
except IntegrityError, e:
except IntegrityError as e:
formatted_traceback = traceback.format_exc()
self.assertIn('obj.save', formatted_traceback)

View File

@@ -35,7 +35,7 @@ class InvalidModelTestCase(unittest.TestCase):
try:
module = load_app("modeltests.invalid_models.invalid_models")
except Exception, e:
except Exception:
self.fail('Unable to load invalid model module')
count = get_validation_errors(self.stdout, module)

View File

@@ -468,13 +468,13 @@ class LookupTests(TestCase):
try:
Article.objects.filter(pub_date_year='2005').count()
self.fail('FieldError not raised')
except FieldError, ex:
except FieldError as ex:
self.assertEqual(str(ex), "Cannot resolve keyword 'pub_date_year' "
"into field. Choices are: author, headline, id, pub_date, tag")
try:
Article.objects.filter(headline__starts='Article')
self.fail('FieldError not raised')
except FieldError, ex:
except FieldError as ex:
self.assertEqual(str(ex), "Join on field 'headline' not permitted. "
"Did you misspell 'starts' for the lookup type?")

View File

@@ -169,10 +169,8 @@ class SelectForUpdateTests(TransactionTestCase):
people[0].name = 'Fred'
people[0].save()
transaction.commit()
except DatabaseError, e:
except DatabaseError as e:
status.append(e)
except Exception, e:
raise
finally:
# This method is run in a separate thread. It uses its own
# database connection. Close it without waiting for the GC.
@@ -246,7 +244,7 @@ class SelectForUpdateTests(TransactionTestCase):
)
)
)
except DatabaseError, e:
except DatabaseError as e:
status.append(e)
finally:
# This method is run in a separate thread. It uses its own

View File

@@ -99,6 +99,6 @@ try:
class MultipleAutoFields(models.Model):
auto1 = models.AutoField(primary_key=True)
auto2 = models.AutoField(primary_key=True)
except AssertionError, assertion_error:
except AssertionError as assertion_error:
pass # Fail silently
assert str(assertion_error) == u"A model can't have more than one AutoField."

View File

@@ -10,13 +10,13 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [u"'foo' value must be an integer."])
# primary_key must be True. Refs #12467.
self.assertRaises(AssertionError, models.AutoField, 'primary_key', False)
try:
models.AutoField(primary_key=False)
except AssertionError, e:
except AssertionError as e:
self.assertEqual(str(e), "AutoFields must have primary_key=True.")
def test_integer_field_raises_error_message(self):
@@ -24,7 +24,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [u"'foo' value must be an integer."])
def test_boolean_field_raises_error_message(self):
@@ -32,7 +32,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages,
[u"'foo' value must be either True or False."])
@@ -41,7 +41,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [u"'foo' value must be a float."])
def test_decimal_field_raises_error_message(self):
@@ -49,7 +49,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages,
[u"'foo' value must be a decimal number."])
@@ -58,7 +58,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages,
[u"'foo' value must be either None, True or False."])
@@ -67,7 +67,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'foo' value has an invalid date format. "
u"It must be in YYYY-MM-DD format."])
@@ -75,7 +75,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'aaaa-10-10', None)
try:
f.clean('aaaa-10-10', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'aaaa-10-10' value has an invalid date format. "
u"It must be in YYYY-MM-DD format."])
@@ -83,7 +83,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, '2011-13-10', None)
try:
f.clean('2011-13-10', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'2011-13-10' value has the correct format (YYYY-MM-DD) "
u"but it is an invalid date."])
@@ -91,7 +91,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, '2011-10-32', None)
try:
f.clean('2011-10-32', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'2011-10-32' value has the correct format (YYYY-MM-DD) "
u"but it is an invalid date."])
@@ -102,7 +102,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'foo' value has an invalid format. It must be "
u"in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."])
@@ -111,7 +111,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, '2011-10-32', None)
try:
f.clean('2011-10-32', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'2011-10-32' value has the correct format "
u"(YYYY-MM-DD) but it is an invalid date."])
@@ -120,7 +120,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, '2011-10-32 10:10', None)
try:
f.clean('2011-10-32 10:10', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'2011-10-32 10:10' value has the correct format "
u"(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) "
@@ -132,7 +132,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, 'foo', None)
try:
f.clean('foo', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'foo' value has an invalid format. It must be in "
u"HH:MM[:ss[.uuuuuu]] format."])
@@ -140,7 +140,7 @@ class ValidationMessagesTest(TestCase):
self.assertRaises(ValidationError, f.clean, '25:50', None)
try:
f.clean('25:50', None)
except ValidationError, e:
except ValidationError as e:
self.assertEqual(e.messages, [
u"'25:50' value has the correct format "
u"(HH:MM[:ss[.uuuuuu]]) but it is an invalid time."])

View File

@@ -115,19 +115,19 @@ class PerformUniqueChecksTest(TestCase):
p = FlexibleDatePost(title="Django 1.0 is released")
try:
p.full_clean()
except ValidationError, e:
except ValidationError:
self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.")
p = FlexibleDatePost(slug="Django 1.0")
try:
p.full_clean()
except ValidationError, e:
except ValidationError:
self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
p = FlexibleDatePost(subtitle="Finally")
try:
p.full_clean()
except ValidationError, e:
except ValidationError:
self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
def test_unique_errors(self):