Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#16664 closed Bug (fixed)

URLField's to_python method fails with ValueError on some urls on python 2.7

Reported by: zigzag Owned by: Chris Beaven
Component: Forms Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This happens cause an urlparse libraries of python 2.7 and <2.7 are very different - in 2.7 python urlparse on invalid IPv6 url (that may be http://[1080:0:0:0:8:800:200C:417A]/index.html - see fails with ValueError, but urlparse at py < 2.7 never raises an exception. For example,

import urlparse
print urlparse.urlsplit('http://[a')

will print SplitResult(scheme='http', netloc='[a', path=, query=, fragment='') on python 2.6 and will raise ValueError("Invalid IPv6 URL") on python 2.7

Attachments (1)

url_field.patch (2.2 KB) - added by zigzag 7 years ago.

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by zigzag

Attachment: url_field.patch added

comment:1 Changed 7 years ago by zigzag

Owner: zigzag deleted

comment:2 Changed 7 years ago by zigzag

I've created a patch for this bug, but it doesn't cover processing ipv6 urls, it only catch ValueError exception from urlparse's urlsplit ibrary.

comment:3 Changed 7 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

zigzag, your report doesn't mention Django at all — I nearly closed the ticket as unrelated to Django! :)

Given the patch, if I understand correctly, the bug is that, since python 2.7, django.forms.fields.URLField.to_python can raise ValueError.

But a form field's to_python method shouldn't raise any exception other than ValidationError.

This rule isn't written explicitly in but it's true for Django's built-in form fields.

comment:4 Changed 7 years ago by zigzag

No, with my patch django.forms.fields.URLField.to_python will catch ValueError from urlparse.urlsplit and raise ValidationError.

Last edited 7 years ago by zigzag (previous) (diff)

comment:5 Changed 7 years ago by zigzag

Somebody, please answer me!

comment:6 Changed 7 years ago by Chris Beaven

Owner: set to Chris Beaven
Resolution: fixed
Status: newclosed

In [16752]:

Fixes #16664 -- URLField's to_python method fails with ValueError on some urls on python 2.7. Based on patch by zigzag.

Note: See TracTickets for help on using tickets.
Back to Top