Author: SmileyChris
Date: 2011-09-09 15:57:12 -0700 (Fri, 09 Sep 2011)
New Revision: 16752

Modified:
   django/trunk/django/forms/fields.py
   django/trunk/tests/regressiontests/forms/tests/fields.py
Log:
Fixes #16664 -- URLField's to_python method fails with ValueError on some urls 
on python 2.7. Based on patch by zigzag.

Modified: django/trunk/django/forms/fields.py
===================================================================
--- django/trunk/django/forms/fields.py 2011-09-09 22:50:03 UTC (rev 16751)
+++ django/trunk/django/forms/fields.py 2011-09-09 22:57:12 UTC (rev 16752)
@@ -583,9 +583,22 @@
         
self.validators.append(validators.URLValidator(verify_exists=verify_exists, 
validator_user_agent=validator_user_agent))
 
     def to_python(self, value):
+
+        def split_url(url):
+            """
+            Returns a list of url parts via ``urlparse.urlsplit`` (or raises a
+            ``ValidationError`` exception for certain).
+            """
+            try:
+                return list(urlparse.urlsplit(url))
+            except ValueError:
+                # urlparse.urlsplit can raise a ValueError with some
+                # misformatted URLs.
+                raise ValidationError(self.error_messages['invalid'])
+
         value = super(URLField, self).to_python(value)
         if value:
-            url_fields = list(urlparse.urlsplit(value))
+            url_fields = split_url(value)
             if not url_fields[0]:
                 # If no URL scheme given, assume http://
                 url_fields[0] = 'http'
@@ -596,8 +609,7 @@
                 url_fields[2] = ''
                 # Rebuild the url_fields list, since the domain segment may now
                 # contain the path too.
-                value = urlparse.urlunsplit(url_fields)
-                url_fields = list(urlparse.urlsplit(value))
+                url_fields = split_url(urlparse.urlunsplit(url_fields))
             if not url_fields[2]:
                 # the path portion may need to be added before query params
                 url_fields[2] = '/'

Modified: django/trunk/tests/regressiontests/forms/tests/fields.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests/fields.py    2011-09-09 
22:50:03 UTC (rev 16751)
+++ django/trunk/tests/regressiontests/forms/tests/fields.py    2011-09-09 
22:57:12 UTC (rev 16752)
@@ -587,6 +587,8 @@
         self.assertEqual(u'http://valid-----hyphens.com/', 
f.clean('http://valid-----hyphens.com'))
         
self.assertEqual(u'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', 
f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah'))
         
self.assertEqual(u'http://www.example.com/s/http://code.djangoproject.com/ticket/13804',
 f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804'))
+        self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", 
f.clean, '[a')
+        self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", 
f.clean, 'http://[a')
 
     def test_url_regex_ticket11198(self):
         f = URLField()

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to