Fixed #7777 -- Added validation handling for NaN, Inf and -Inf in DecimalFields. Thanks to thebitguru for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12490 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e6db084ac8
commit
692fd7da5e
|
@ -283,6 +283,11 @@ class DecimalField(Field):
|
||||||
super(DecimalField, self).validate(value)
|
super(DecimalField, self).validate(value)
|
||||||
if value in validators.EMPTY_VALUES:
|
if value in validators.EMPTY_VALUES:
|
||||||
return
|
return
|
||||||
|
# Check for NaN, Inf and -Inf values. We can't compare directly for NaN,
|
||||||
|
# since it is never equal to itself. However, NaN is the only value that
|
||||||
|
# isn't equal to itself, so we can use this to identify NaN
|
||||||
|
if value != value or value == Decimal("Inf") or value == Decimal("-Inf"):
|
||||||
|
raise ValidationError(self.error_messages['invalid'])
|
||||||
sign, digittuple, exponent = value.as_tuple()
|
sign, digittuple, exponent = value.as_tuple()
|
||||||
decimals = abs(exponent)
|
decimals = abs(exponent)
|
||||||
# digittuple doesn't include any leading zeros.
|
# digittuple doesn't include any leading zeros.
|
||||||
|
|
|
@ -203,6 +203,9 @@ class FieldsTests(TestCase):
|
||||||
self.assertEqual(f.clean('3.14'), Decimal("3.14"))
|
self.assertEqual(f.clean('3.14'), Decimal("3.14"))
|
||||||
self.assertEqual(f.clean(3.14), Decimal("3.14"))
|
self.assertEqual(f.clean(3.14), Decimal("3.14"))
|
||||||
self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14"))
|
self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14"))
|
||||||
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN')
|
||||||
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf')
|
||||||
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf')
|
||||||
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')
|
||||||
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')
|
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')
|
||||||
self.assertEqual(f.clean('1.0 '), Decimal("1.0"))
|
self.assertEqual(f.clean('1.0 '), Decimal("1.0"))
|
||||||
|
|
Loading…
Reference in New Issue