Skip to content

Commit 5ea9734

Browse files
committed
FloatField will crash if the input is a number that is too big
1 parent 2510456 commit 5ea9734

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

rest_framework/fields.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def get_attribute(instance, attrs):
104104
# If we raised an Attribute or KeyError here it'd get treated
105105
# as an omitted field in `Field.get_attribute()`. Instead we
106106
# raise a ValueError to ensure the exception is not masked.
107-
raise ValueError('Exception raised in callable attribute "{}"; original exception was: {}'.format(attr, exc))
107+
raise ValueError(
108+
'Exception raised in callable attribute "{}"; original exception was: {}'.format(attr, exc))
108109

109110
return instance
110111

@@ -181,6 +182,7 @@ def iter_options(grouped_choices, cutoff=None, cutoff_text=None):
181182
"""
182183
Helper function for options and option groups in templates.
183184
"""
185+
184186
class StartOptionGroup:
185187
start_option_group = True
186188
end_option_group = False
@@ -366,10 +368,10 @@ def bind(self, field_name, parent):
366368
# 'source' argument has been used. For example:
367369
# my_field = serializer.CharField(source='my_field')
368370
assert self.source != field_name, (
369-
"It is redundant to specify `source='%s'` on field '%s' in "
370-
"serializer '%s', because it is the same as the field name. "
371-
"Remove the `source` keyword argument." %
372-
(field_name, self.__class__.__name__, parent.__class__.__name__)
371+
"It is redundant to specify `source='%s'` on field '%s' in "
372+
"serializer '%s', because it is the same as the field name. "
373+
"Remove the `source` keyword argument." %
374+
(field_name, self.__class__.__name__, parent.__class__.__name__)
373375
)
374376

375377
self.field_name = field_name
@@ -794,7 +796,8 @@ def __init__(self, allow_unicode=False, **kwargs):
794796
super().__init__(**kwargs)
795797
self.allow_unicode = allow_unicode
796798
if self.allow_unicode:
797-
validator = RegexValidator(re.compile(r'^[-\w]+\Z', re.UNICODE), message=self.error_messages['invalid_unicode'])
799+
validator = RegexValidator(re.compile(r'^[-\w]+\Z', re.UNICODE),
800+
message=self.error_messages['invalid_unicode'])
798801
else:
799802
validator = RegexValidator(re.compile(r'^[-a-zA-Z0-9_]+$'), message=self.error_messages['invalid'])
800803
self.validators.append(validator)
@@ -943,7 +946,7 @@ def to_internal_value(self, data):
943946

944947
try:
945948
return float(data)
946-
except (TypeError, ValueError):
949+
except (TypeError, ValueError, OverflowError):
947950
self.fail('invalid')
948951

949952
def to_representation(self, value):
@@ -994,7 +997,7 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
994997
if rounding is not None:
995998
valid_roundings = [v for k, v in vars(decimal).items() if k.startswith('ROUND_')]
996999
assert rounding in valid_roundings, (
997-
'Invalid rounding option %s. Valid values for rounding are: %s' % (rounding, valid_roundings))
1000+
'Invalid rounding option %s. Valid values for rounding are: %s' % (rounding, valid_roundings))
9981001
self.rounding = rounding
9991002

10001003
def validate_empty_values(self, data):
@@ -1732,6 +1735,7 @@ def __new__(cls, value):
17321735
ret = str.__new__(cls, value)
17331736
ret.is_json_string = True
17341737
return ret
1738+
17351739
return JSONString(dictionary[self.field_name])
17361740
return dictionary.get(self.field_name, empty)
17371741

@@ -1785,6 +1789,7 @@ class HiddenField(Field):
17851789
constraint on a pair of fields, as we need some way to include the date in
17861790
the validated data.
17871791
"""
1792+
17881793
def __init__(self, **kwargs):
17891794
assert 'default' in kwargs, 'default is a required argument.'
17901795
kwargs['write_only'] = True
@@ -1814,6 +1819,7 @@ class ExampleSerializer(Serializer):
18141819
def get_extra_info(self, obj):
18151820
return ... # Calculate some data to return.
18161821
"""
1822+
18171823
def __init__(self, method_name=None, **kwargs):
18181824
self.method_name = method_name
18191825
kwargs['source'] = '*'

0 commit comments

Comments
 (0)