From aa349fe76729dbea1b8becf1846ce58c70871f35 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Aug 2016 16:14:26 +0100 Subject: [PATCH] Handle non-string input for IP fields (#4338) --- rest_framework/fields.py | 5 ++++- tests/test_fields.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 46d7ed09b..69cf9740b 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -804,7 +804,10 @@ class IPAddressField(CharField): self.validators.extend(validators) def to_internal_value(self, data): - if data and ':' in data: + if not isinstance(data, six.string_types): + self.fail('invalid', value=data) + + if ':' in data: try: if self.protocol in ('both', 'ipv6'): return clean_ipv6_address(data, self.unpack_ipv4) diff --git a/tests/test_fields.py b/tests/test_fields.py index 1149cc4b3..24ff25588 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -663,6 +663,7 @@ class TestIPAddressField(FieldValues): '127.122.111.2231': ['Enter a valid IPv4 or IPv6 address.'], '2001:::9652': ['Enter a valid IPv4 or IPv6 address.'], '2001:0db8:85a3:0042:1000:8a2e:0370:73341': ['Enter a valid IPv4 or IPv6 address.'], + 1000: ['Enter a valid IPv4 or IPv6 address.'], } outputs = {} field = serializers.IPAddressField()