From 32acc4a72bf62d9790be832504746054d6ef5ce5 Mon Sep 17 00:00:00 2001 From: Ryan Allen Date: Tue, 21 Apr 2015 10:48:12 -0400 Subject: [PATCH] Set IntegerField class variable for compiled decimal regex, comment for purpose --- rest_framework/fields.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index ed0270a59..68835bded 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -665,6 +665,7 @@ class IntegerField(Field): 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. + re_decimal = re.compile(r'\.0*\s*$') # allow e.g. '1.0' as an int, but not '1.2' def __init__(self, **kwargs): self.max_value = kwargs.pop('max_value', None) @@ -682,7 +683,7 @@ class IntegerField(Field): self.fail('max_string_length') try: - data = int(re.compile(r'\.0*\s*$').sub('', str(data))) + data = int(self.re_decimal.sub('', str(data))) except (ValueError, TypeError): self.fail('invalid') return data