Commit 8b415e72 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

[1.1.X] Fixed #7777 -- Added validation handling for NaN, Inf and -Inf in...

[1.1.X] Fixed #7777 -- Added validation handling for NaN, Inf and -Inf in DecimalFields. Thanks to thebitguru for the patch.

Backport of r12490 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12491 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 9ee2d5c6
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -240,6 +240,12 @@ class DecimalField(Field):
        except DecimalException:
            raise ValidationError(self.error_messages['invalid'])

        # 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.
+12 −0
Original line number Diff line number Diff line
@@ -320,6 +320,18 @@ True
True
>>> f.clean(Decimal('3.14')) == Decimal("3.14")
True
>>> f.clean('NaN')
Traceback (most recent call last):
...
ValidationError: [u'Enter a number.']
>>> f.clean('Inf')
Traceback (most recent call last):
...
ValidationError: [u'Enter a number.']
>>> f.clean('-Inf')
Traceback (most recent call last):
...
ValidationError: [u'Enter a number.']
>>> f.clean('a')
Traceback (most recent call last):
...