Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #7777 -- Added validation handling for NaN, Inf and -Inf in Dec…

…imalFields. Thanks to thebitguru for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12490 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 692fd7da5e5bfe1533c40f94999ea42f9b986356 1 parent e6db084
Russell Keith-Magee freakboy3742 authored
Showing with 11 additions and 3 deletions.
  1. +7 −2 django/forms/fields.py
  2. +4 −1 tests/regressiontests/forms/fields.py
9 django/forms/fields.py
View
@@ -283,6 +283,11 @@ def validate(self, value):
super(DecimalField, self).validate(value)
if value in validators.EMPTY_VALUES:
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()
decimals = abs(exponent)
# digittuple doesn't include any leading zeros.
@@ -467,7 +472,7 @@ def to_python(self, data):
f = super(ImageField, self).to_python(data)
if f is None:
return None
-
+
# Try to import PIL in either of the two ways it can end up installed.
try:
from PIL import Image
@@ -584,7 +589,7 @@ class ChoiceField(Field):
def __init__(self, choices=(), required=True, widget=None, label=None,
initial=None, help_text=None, *args, **kwargs):
- super(ChoiceField, self).__init__(required=required, widget=widget, label=label,
+ super(ChoiceField, self).__init__(required=required, widget=widget, label=label,
initial=initial, help_text=help_text, *args, **kwargs)
self.choices = choices
5 tests/regressiontests/forms/fields.py
View
@@ -203,6 +203,9 @@ def test_decimalfield_13(self):
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.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, u'łąść')
self.assertEqual(f.clean('1.0 '), Decimal("1.0"))
@@ -498,7 +501,7 @@ def test_url_regex_ticket11198(self):
# hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,))
- # a second test, to make sure the problem is really addressed, even on
+ # a second test, to make sure the problem is really addressed, even on
# domains that don't fail the domain label length check in the regex
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,))
Please sign in to comment.
Something went wrong with that request. Please try again.