summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSrinivas Reddy Thatiparthy <srinivasreddy@users.noreply.github.com>2017-08-10 06:11:39 +0530
committerTim Graham <timograham@gmail.com>2017-08-09 20:41:39 -0400
commit5cb7619995bd8df2969d4e92984768a4f14af89b (patch)
tree117b34a3dfc536266358c541b410329f2c895623
parent5244d7cf5403df2d565e235f5051229973c3033b (diff)
Simplified Float/DecimalField.validate() with math.isfinite().
-rw-r--r--django/forms/fields.py14
1 files changed, 5 insertions, 9 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 3750e85af1..8494e316dd 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -5,6 +5,7 @@ Field classes.
import copy
import datetime
import itertools
+import math
import os
import re
import uuid
@@ -306,13 +307,11 @@ class FloatField(IntegerField):
def validate(self, value):
super().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')):
+ if value in self.empty_values:
+ return
+ if not math.isfinite(value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
- return value
-
def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
if isinstance(widget, NumberInput) and 'step' not in widget.attrs:
@@ -352,10 +351,7 @@ class DecimalField(IntegerField):
super().validate(value)
if value in self.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"):
+ if not math.isfinite(value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
def widget_attrs(self, widget):