problem with bytes and str in validators

Issue #53 resolved
Johan Harjono
created an issue

{{{

!python

====================================================================== ERROR: test_EmailValidator_raises_error_11 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 103, in call super(EmailValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid e-mail address.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 113, in call super(EmailValidator, self).call('@'.join(parts)) TypeError: sequence item 1: expected str instance, bytes found

====================================================================== ERROR: test_EmailValidator_raises_error_12 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 103, in call super(EmailValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid e-mail address.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 113, in call super(EmailValidator, self).call('@'.join(parts)) TypeError: sequence item 2: expected str instance, bytes found

====================================================================== ERROR: test_URLValidator_raises_error_70 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 58, in call super(URLValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid value.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 68, in call url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) File "/usr/lib/python3.1/urllib/parse.py", line 195, in urlunsplit url = '//' + (netloc or '') + url TypeError: Can't convert 'bytes' object to str implicitly

====================================================================== ERROR: test_URLValidator_raises_error_71 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 58, in call super(URLValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid value.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 68, in call url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) File "/usr/lib/python3.1/urllib/parse.py", line 195, in urlunsplit url = '//' + (netloc or '') + url TypeError: Can't convert 'bytes' object to str implicitly

====================================================================== ERROR: test_URLValidator_raises_error_73 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 58, in call super(URLValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid value.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 68, in call url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) File "/usr/lib/python3.1/urllib/parse.py", line 195, in urlunsplit url = '//' + (netloc or '') + url TypeError: Can't convert 'bytes' object to str implicitly

====================================================================== ERROR: test_URLValidator_raises_error_74 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 58, in call super(URLValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid value.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 68, in call url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) File "/usr/lib/python3.1/urllib/parse.py", line 195, in urlunsplit url = '//' + (netloc or '') + url TypeError: Can't convert 'bytes' object to str implicitly

====================================================================== ERROR: test_URLValidator_raises_error_75 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 58, in call super(URLValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid value.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 68, in call url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) File "/usr/lib/python3.1/urllib/parse.py", line 195, in urlunsplit url = '//' + (netloc or '') + url TypeError: Can't convert 'bytes' object to str implicitly

====================================================================== ERROR: test_URLValidator_raises_error_76 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 58, in call super(URLValidator, self).call(value) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 39, in call raise ValidationError(self.message, code=self.code) django.core.exceptions.ValidationError: ['Enter a valid value.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) File "/usr/lib/python3.1/unittest.py", line 589, in assertRaises callableObj(args, *kwargs) File "/usr/local/lib/python3.1/dist-packages/django/core/validators.py", line 68, in call url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) File "/usr/lib/python3.1/urllib/parse.py", line 195, in urlunsplit url = '//' + (netloc or '') + url TypeError: Can't convert 'bytes' object to str implicitly

====================================================================== FAIL: test_RegexValidator_raises_error_21 (modeltests.validators.tests.TestSimpleValidators)


Traceback (most recent call last): File "/home/johan/workspace/fiji/build/tests/modeltests/validators/tests.py", line 125, in test_func self.assertRaises(expected, validator, value) AssertionError: ValidationError not raised by <django.core.validators.RegexValidator object at 0x8f98a2c>


}}}

Comments (2)

  1. Nate Margono

    just an update, the error (in error_70) lies in the line within the function urlunsplit() in parse.py:

    scheme, netloc, url, query, fragment = components
    

    where netloc is the only bytestring from the quintuplets. do i convert it to str before it is passed into this function? or within this function would do the trick just fine...

  2. Nate Margono

    The problem can be traced back on lines 78 and 126 in django/core/validators.py, when encoding a str to IDN->ACE format. In Py2.x, this will convert the string into a str object, whereas in Py3.x it will convert to a bytes object. We want the conversion to be a str obj, so I added a similar fix from issue #44 that converts these strings to str only when done in Py3.x version of Django. This issue is now closed.

    PS: i apologize for my name not showing up properly on the Changesets. i committed and pushed with Eclipse and it uses my login screen name as the username. i think i fixed that now.

  3. Log in to comment