From b7fd668b37341fc92d67c4854c4f244e10895c9b Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Sat, 21 Aug 2021 20:57:15 +0530 Subject: [PATCH] Fixed #33033 -- Prevented models.DecimalField from accepting NaN values. --- django/db/models/fields/__init__.py | 7 +++++++ tests/model_fields/test_decimalfield.py | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index e79f4acf6c..0e72a09e59 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2,6 +2,7 @@ import collections.abc import copy import datetime import decimal +import math import operator import uuid import warnings @@ -1539,6 +1540,12 @@ class DecimalField(Field): if value is None: return value if isinstance(value, float): + if math.isnan(value): + raise exceptions.ValidationError( + self.error_messages['invalid'], + code='invalid', + params={'value': value}, + ) return self.context.create_decimal_from_float(value) try: return decimal.Decimal(value) diff --git a/tests/model_fields/test_decimalfield.py b/tests/model_fields/test_decimalfield.py index 2d96cfd437..ea06ab9fa2 100644 --- a/tests/model_fields/test_decimalfield.py +++ b/tests/model_fields/test_decimalfield.py @@ -1,3 +1,4 @@ +import math from decimal import Decimal from django.core import validators @@ -65,6 +66,13 @@ class DecimalFieldTests(TestCase): bd = BigD.objects.get(pk=bd.pk) self.assertEqual(bd.d, Decimal('12.9')) + def test_save_nan_invalid(self): + msg = '“nan” value must be a decimal number.' + with self.assertRaisesMessage(ValidationError, msg): + BigD.objects.create(d=float('nan')) + with self.assertRaisesMessage(ValidationError, msg): + BigD.objects.create(d=math.nan) + def test_fetch_from_db_without_float_rounding(self): big_decimal = BigD.objects.create(d=Decimal('.100000000000000000000000000005')) big_decimal.refresh_from_db()