Skip to content

Commit 692fd7d

Browse files
committed
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
1 parent e6db084 commit 692fd7d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

django/forms/fields.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ def validate(self, value):
283283
super(DecimalField, self).validate(value)
284284
if value in validators.EMPTY_VALUES:
285285
return
286+
# Check for NaN, Inf and -Inf values. We can't compare directly for NaN,
287+
# since it is never equal to itself. However, NaN is the only value that
288+
# isn't equal to itself, so we can use this to identify NaN
289+
if value != value or value == Decimal("Inf") or value == Decimal("-Inf"):
290+
raise ValidationError(self.error_messages['invalid'])
286291
sign, digittuple, exponent = value.as_tuple()
287292
decimals = abs(exponent)
288293
# digittuple doesn't include any leading zeros.
@@ -467,7 +472,7 @@ def to_python(self, data):
467472
f = super(ImageField, self).to_python(data)
468473
if f is None:
469474
return None
470-
475+
471476
# Try to import PIL in either of the two ways it can end up installed.
472477
try:
473478
from PIL import Image
@@ -584,7 +589,7 @@ class ChoiceField(Field):
584589

585590
def __init__(self, choices=(), required=True, widget=None, label=None,
586591
initial=None, help_text=None, *args, **kwargs):
587-
super(ChoiceField, self).__init__(required=required, widget=widget, label=label,
592+
super(ChoiceField, self).__init__(required=required, widget=widget, label=label,
588593
initial=initial, help_text=help_text, *args, **kwargs)
589594
self.choices = choices
590595

tests/regressiontests/forms/fields.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def test_decimalfield_13(self):
203203
self.assertEqual(f.clean('3.14'), Decimal("3.14"))
204204
self.assertEqual(f.clean(3.14), Decimal("3.14"))
205205
self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14"))
206+
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN')
207+
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf')
208+
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf')
206209
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')
207210
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')
208211
self.assertEqual(f.clean('1.0 '), Decimal("1.0"))
@@ -498,7 +501,7 @@ def test_url_regex_ticket11198(self):
498501
# hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
499502
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,))
500503

501-
# a second test, to make sure the problem is really addressed, even on
504+
# a second test, to make sure the problem is really addressed, even on
502505
# domains that don't fail the domain label length check in the regex
503506
self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,))
504507

0 commit comments

Comments
 (0)