From 43397a81aec2b10ece63099f9f89c51d56785c35 Mon Sep 17 00:00:00 2001 From: Sergey Date: Tue, 22 Oct 2019 12:06:37 +0300 Subject: [PATCH] Fixed decimal snan deserialization (#7002) * Added test case causes exception in DecimalField deserialization * Fixed NaN checking which throws exception with sNaN value --- rest_framework/fields.py | 4 +--- tests/test_fields.py | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index c0ceebe3b..ea8f47b2d 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1062,9 +1062,7 @@ class DecimalField(Field): except decimal.DecimalException: self.fail('invalid') - # Check for NaN. It is the only value that isn't equal to itself, - # so we can use this to identify NaN values. - if value != value: + if value.is_nan(): self.fail('invalid') # Check for infinity and negative infinity. diff --git a/tests/test_fields.py b/tests/test_fields.py index 7c495cd63..1d302b730 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1080,6 +1080,7 @@ class TestDecimalField(FieldValues): invalid_inputs = ( ('abc', ["A valid number is required."]), (Decimal('Nan'), ["A valid number is required."]), + (Decimal('Snan'), ["A valid number is required."]), (Decimal('Inf'), ["A valid number is required."]), ('12.345', ["Ensure that there are no more than 3 digits in total."]), (200000000000.0, ["Ensure that there are no more than 3 digits in total."]),