Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored February 22, 2010
9  django/forms/fields.py
@@ -283,6 +283,11 @@ def validate(self, value):
283 283
         super(DecimalField, self).validate(value)
284 284
         if value in validators.EMPTY_VALUES:
285 285
             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'])
286 291
         sign, digittuple, exponent = value.as_tuple()
287 292
         decimals = abs(exponent)
288 293
         # digittuple doesn't include any leading zeros.
@@ -467,7 +472,7 @@ def to_python(self, data):
467 472
         f = super(ImageField, self).to_python(data)
468 473
         if f is None:
469 474
             return None
470  
-            
  475
+
471 476
         # Try to import PIL in either of the two ways it can end up installed.
472 477
         try:
473 478
             from PIL import Image
@@ -584,7 +589,7 @@ class ChoiceField(Field):
584 589
 
585 590
     def __init__(self, choices=(), required=True, widget=None, label=None,
586 591
                  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,
588 593
                                         initial=initial, help_text=help_text, *args, **kwargs)
589 594
         self.choices = choices
590 595
 
5  tests/regressiontests/forms/fields.py
@@ -203,6 +203,9 @@ def test_decimalfield_13(self):
203 203
         self.assertEqual(f.clean('3.14'), Decimal("3.14"))
204 204
         self.assertEqual(f.clean(3.14), Decimal("3.14"))
205 205
         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')
206 209
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')
207 210
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')
208 211
         self.assertEqual(f.clean('1.0 '), Decimal("1.0"))
@@ -498,7 +501,7 @@ def test_url_regex_ticket11198(self):
498 501
         # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
499 502
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,))
500 503
 
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
502 505
         # domains that don't fail the domain label length check in the regex
503 506
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,))
504 507
 

0 notes on commit 692fd7d

Please sign in to comment.
Something went wrong with that request. Please try again.