#24708: forms.GenericIPAddressField.to_python behaves differently to other
CharField subclasses.
----------------------------+--------------------
     Reporter:  kezabelle   |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Forms       |    Version:  master
     Severity:  Normal      |   Keywords:
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+--------------------
 Other `forms.fields.CharField` subclasses (`UUIDField`, `URLField`), when
 they replace the `to_python` method, call `super(...,
 self).to_python(...)` before acting on the value.

 `GenericIPAddressField` however,
 [https://github.com/django/django/blob/master/django/forms/fields.py#L1220
 does not], which means it calls `.strip()` on the input, assuming the
 input is a string.

 Thus, the following fails:
 {{{
 >>> GenericIPAddressField().to_python(1)
 AttributeError: 'int' object has no attribute 'strip'
 }}}
 where the other aforementioned subclasses succeed in their functionality:
 {{{
 >>> URLField().to_python(1)
 'http://1'
 >>> UUIDField().to_python(1)
 ValidationError: ['Enter a valid UUID.']
 }}}
 Because the call to `super(...)` ensures either `''` or `smart_text(...)`
 is returned.

 I ''think'' the fix is thus:
 {{{
     def to_python(self, value):
         value = super(GenericIPAddressField,
 self).to_python(value).strip()  # this is the fix line.
         if value in self.empty_values:
             return ''
         if value and ':' in value:
             return clean_ipv6_address(value, self.unpack_ipv4)
         return value
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24708>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.32a65e622cb30423436db2ae7fa911df%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to