Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

NaN, +Inf, and -Inf are no longer valid values for FloatFields.
  • Loading branch information...
commit cc957cb16cfdad7e6c9e97dc885fc415abbf5eaa 1 parent cd4068f
@dlanger dlanger authored timgraham committed
Showing with 12 additions and 0 deletions.
  1. +9 −0 django/forms/fields.py
  2. +3 −0  tests/forms_tests/tests/test_fields.py
View
9 django/forms/fields.py
@@ -279,6 +279,15 @@ def to_python(self, value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
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):
attrs = super(FloatField, self).widget_attrs(widget)
if isinstance(widget, NumberInput):
View
3  tests/forms_tests/tests/test_fields.py
@@ -254,6 +254,9 @@ def test_floatfield_1(self):
self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, '1.0a')
self.assertEqual(f.max_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):
f = FloatField(required=False)
Please sign in to comment.
Something went wrong with that request. Please try again.