From 5cb7619995bd8df2969d4e92984768a4f14af89b Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 10 Aug 2017 06:11:39 +0530 Subject: [PATCH] Simplified Float/DecimalField.validate() with math.isfinite(). --- django/forms/fields.py | 14 +++++--------- 1 file 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):