Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #16664 -- URLField's to_python method fails with ValueError on …

…some urls on python 2.7. Based on patch by zigzag.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16752 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 470c70f876b2fc9c7783a45589f7bfd1c4356078 1 parent c4d8b4c
Chris Beaven authored September 09, 2011
18  django/forms/fields.py
@@ -583,9 +583,22 @@ def __init__(self, max_length=None, min_length=None, verify_exists=False,
583 583
         self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent))
584 584
 
585 585
     def to_python(self, value):
  586
+
  587
+        def split_url(url):
  588
+            """
  589
+            Returns a list of url parts via ``urlparse.urlsplit`` (or raises a
  590
+            ``ValidationError`` exception for certain).
  591
+            """
  592
+            try:
  593
+                return list(urlparse.urlsplit(url))
  594
+            except ValueError:
  595
+                # urlparse.urlsplit can raise a ValueError with some
  596
+                # misformatted URLs.
  597
+                raise ValidationError(self.error_messages['invalid'])
  598
+
586 599
         value = super(URLField, self).to_python(value)
587 600
         if value:
588  
-            url_fields = list(urlparse.urlsplit(value))
  601
+            url_fields = split_url(value)
589 602
             if not url_fields[0]:
590 603
                 # If no URL scheme given, assume http://
591 604
                 url_fields[0] = 'http'
@@ -596,8 +609,7 @@ def to_python(self, value):
596 609
                 url_fields[2] = ''
597 610
                 # Rebuild the url_fields list, since the domain segment may now
598 611
                 # contain the path too.
599  
-                value = urlparse.urlunsplit(url_fields)
600  
-                url_fields = list(urlparse.urlsplit(value))
  612
+                url_fields = split_url(urlparse.urlunsplit(url_fields))
601 613
             if not url_fields[2]:
602 614
                 # the path portion may need to be added before query params
603 615
                 url_fields[2] = '/'
2  tests/regressiontests/forms/tests/fields.py
@@ -587,6 +587,8 @@ def test_urlfield_1(self):
587 587
         self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com'))
588 588
         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'))
589 589
         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'))
  590
+        self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '[a')
  591
+        self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://[a')
590 592
 
591 593
     def test_url_regex_ticket11198(self):
592 594
         f = URLField()

0 notes on commit 470c70f

Please sign in to comment.
Something went wrong with that request. Please try again.