Ticket #20510: 20510-1.diff

File 20510-1.diff, 4.3 KB (added by Claude Paroz, 11 years ago)

Loud failing for non-ascii bytestrings

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index ac68b9f..2ed5c4c 100644
    a b from django.forms.widgets import (  
    2626    SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION
    2727)
    2828from django.utils import formats
    29 from django.utils.encoding import smart_text, force_str, force_text
     29from django.utils.encoding import smart_text, force_str, force_text, valid_text
    3030from django.utils.ipv6 import clean_ipv6_address
    3131from django.utils import six
    3232from django.utils.translation import ugettext_lazy as _, ungettext_lazy
    class Field(object):  
    8282        #                        hidden widget with initial value after widget.
    8383        # validators -- List of addtional validators to use
    8484        # localize -- Boolean that specifies if the field should be localized.
     85        if label is not None:
     86            label = valid_text(label)
    8587        self.required, self.label, self.initial = required, label, initial
    8688        self.show_hidden_initial = show_hidden_initial
    87         self.help_text = help_text
     89        self.help_text = valid_text(help_text)
    8890        widget = widget or self.widget
    8991        if isinstance(widget, type):
    9092            widget = widget()
  • django/utils/encoding.py

    diff --git a/django/utils/encoding.py b/django/utils/encoding.py
    index adab0d0..400b7b9 100644
    a b def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):  
    170170    else:
    171171        return s.encode(encoding, errors)
    172172
     173def valid_text(value):
     174    """
     175    Check that value is either ascii bytestring or unicode non-ascii.
     176    """
     177    if isinstance(value, bytes):
     178        try:
     179            six.text_type(value, 'ascii')
     180        except UnicodeDecodeError:
     181            raise AssertionError("Strings containing non-ASCII chars should be unicode")
     182    return value
     183
     184
    173185if six.PY3:
    174186    smart_str = smart_text
    175187    force_str = force_text
  • docs/releases/1.6.txt

    diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt
    index 825c5dc..c4bce9c 100644
    a b Miscellaneous  
    582582  changes in 1.6 particularly affect :class:`~django.forms.DecimalField` and
    583583  :class:`~django.forms.ModelMultipleChoiceField`.
    584584
     585* Non-ASCII bytestrings are not accepted any longer for the ``label`` and
     586  ``help_text`` arguments to form fields ``__init__``.
     587
    585588* There have been changes in the way timeouts are handled in cache backends.
    586589  Explicitly passing in ``timeout=None`` no longer results in using the
    587590  default timeout. It will now set a non-expiring timeout. Passing 0 into the
  • tests/forms_tests/tests/test_forms.py

    diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
    index 1a3fb44..911ea23 100644
    a b class FormsTestCase(TestCase):  
    979979<p><label for="id_q4">Answer this question!</label> <input type="text" name="q4" id="id_q4" /></p>
    980980<p><label for="id_q5">The last question. Period.</label> <input type="text" name="q5" id="id_q5" /></p>""")
    981981
     982        # Label should not be a non-ascii bytestring
     983        with self.assertRaises(AssertionError):
     984            class UserRegistration(Form):
     985                username = CharField(max_length=10)
     986                password = CharField(widget=PasswordInput, label=b'Contrase\xc3\xb1a')
     987
    982988        # If a label is set to the empty string for a field, that field won't get a label.
    983989        class UserRegistration(Form):
    984990            username = CharField(max_length=10, label='')
    class FormsTestCase(TestCase):  
    12641270        self.assertHTMLEqual(p.as_ul(), """<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
    12651271<li>Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>""")
    12661272
     1273        # help_text should not be a non-ascii bytestring
     1274        with self.assertRaises(AssertionError):
     1275            class UserRegistration(Form):
     1276                username = CharField(max_length=10, help_text=b'W\xc3\xa4hlen Sie bitte')
     1277
    12671278    def test_subclassing_forms(self):
    12681279        # You can subclass a Form to add fields. The resulting form subclass will have
    12691280        # all of the fields of the parent Form, plus whichever fields you define in the
Back to Top