Commits

nm  committed cbf2ed5

Fixed issue #53 with the bytes to str errors in EmailValidator() and URLValidator().

  • Participants
  • Parent commits 50cddc8

Comments (0)

Files changed (1)

File django/core/validators.py

+import sys
 import re
 import urlparse
 
     # It's OK if Django settings aren't configured.
     URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
 
+###############################################
+# Converters from a byte objects to strings #
+###############################################
+
+def py3_string_conversion(s):
+    # Convert byte s, if any, to str
+    if not isinstance(s, str) and sys.version_info >= (3,):
+        s = str(s, encoding='utf8')
+    return s
+
+###############################################
+
 class RegexValidator(object):
     regex = ''
     message = _(u'Enter a valid value.')
                 scheme, netloc, path, query, fragment = urlparse.urlsplit(value)
                 try:
                     netloc = netloc.encode('idna') # IDN -> ACE
+                    
+                    # only encode netloc to str in Py3.x
+                    netloc = py3_string_conversion(netloc)
                 except UnicodeError: # invalid domain part
                     raise e
                 url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
                 domain_part = parts[-1]
                 try:
                     parts[-1] = parts[-1].encode('idna')
+
+                    # only encode parts[-1] to str in Py3.x
+                    parts[-1] = py3_string_conversion(parts[-1])
                 except UnicodeError:
                     raise e
                 super(EmailValidator, self).__call__(u'@'.join(parts))