Fixed #4287 -- Fixed NaN and +/- Infinity handling in FloatField

NaN, +Inf, and -Inf are no longer valid values for FloatFields.
This commit is contained in:
Daniel Langer 2013-09-06 16:54:48 +00:00 committed by Tim Graham
parent cd4068f359
commit cc957cb16c
2 changed files with 12 additions and 0 deletions

View File

@ -279,6 +279,15 @@ class FloatField(IntegerField):
raise ValidationError(self.error_messages['invalid'], code='invalid') raise ValidationError(self.error_messages['invalid'], code='invalid')
return value return value
def validate(self, value):
super(FloatField, self).validate(value)
# Check for NaN (which is the only thing not equal to itself) and +/- infinity
if value != value or value in (Decimal('Inf'), Decimal('-Inf')):
raise ValidationError(self.error_messages['invalid'], code='invalid')
return value
def widget_attrs(self, widget): def widget_attrs(self, widget):
attrs = super(FloatField, self).widget_attrs(widget) attrs = super(FloatField, self).widget_attrs(widget)
if isinstance(widget, NumberInput): if isinstance(widget, NumberInput):

View File

@ -254,6 +254,9 @@ class FieldsTests(SimpleTestCase):
self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, '1.0a') self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, '1.0a')
self.assertEqual(f.max_value, None) self.assertEqual(f.max_value, None)
self.assertEqual(f.min_value, None) self.assertEqual(f.min_value, None)
self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, 'Infinity')
self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, 'NaN')
self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, '-Inf')
def test_floatfield_2(self): def test_floatfield_2(self):
f = FloatField(required=False) f = FloatField(required=False)