diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 7f4c83b5df..0a37eb0931 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -919,7 +919,8 @@ class FloatField(Field): 'invalid': _('A valid number is required.'), 'max_value': _('Ensure this value is less than or equal to {max_value}.'), 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), - 'max_string_length': _('String value too large.') + 'max_string_length': _('String value too large.'), + 'overflow': _('Integer value too large to convert to float') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -945,6 +946,8 @@ def to_internal_value(self, data): return float(data) except (TypeError, ValueError): self.fail('invalid') + except OverflowError: + self.fail('overflow') def to_representation(self, value): return float(value) diff --git a/tests/test_fields.py b/tests/test_fields.py index 11e293107d..12a9ac7c7c 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,4 +1,5 @@ import datetime +import math import os import re import uuid @@ -1072,6 +1073,14 @@ class TestMinMaxFloatField(FieldValues): field = serializers.FloatField(min_value=1, max_value=3) +class TestFloatFieldOverFlowError(TestCase): + def test_overflow_error_float_field(self): + field = serializers.FloatField() + with pytest.raises(serializers.ValidationError) as exec_info: + field.to_internal_value(data=math.factorial(171)) + assert "Integer value too large to convert to float" in str(exec_info.value.detail) + + class TestDecimalField(FieldValues): """ Valid and invalid values for `DecimalField`. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy