Skip to content

Commit 0ca1145

Browse files
committed
Merge pull request #2853 from ryangallen/master
Set IntegerField class variable for compiled decimal regex, comment for ...
2 parents 605369e + 32acc4a commit 0ca1145

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

rest_framework/fields.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ class IntegerField(Field):
665665
'max_string_length': _('String value too large.')
666666
}
667667
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
668+
re_decimal = re.compile(r'\.0*\s*$') # allow e.g. '1.0' as an int, but not '1.2'
668669

669670
def __init__(self, **kwargs):
670671
self.max_value = kwargs.pop('max_value', None)
@@ -682,7 +683,7 @@ def to_internal_value(self, data):
682683
self.fail('max_string_length')
683684

684685
try:
685-
data = int(re.compile(r'\.0*\s*$').sub('', str(data)))
686+
data = int(self.re_decimal.sub('', str(data)))
686687
except (ValueError, TypeError):
687688
self.fail('invalid')
688689
return data

0 commit comments

Comments
 (0)