Django

Code

Ticket #4291 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

Unicode bug with gettext variables in newforms/widgets.py

Reported by: Fidel Ramos <framos@yaco.es> Assigned to: adrian
Milestone: Component: Forms
Version: SVN Keywords: unicode gettext widget unicode-branch
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

newforms/widgets.py has a bug related to gettext. I have a select widget in a form template with several options, translated with gettext_lazy. The rendering of the template fails with an UnicodeDecodeError exception with a message like

'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128)

Following the callback, trouble arises from calls like this in newforms/widgets.py:

escape(smart_unicode(string_variable_with_non_english_chars_translated_by_gettext))

The fix is simple: exchange the order of the calls to escape() and smart_unicode():

smart_unicode(escape(string_variable_with_non_english_chars_translated_by_gettext))

These are some code snippets that mimic the application in which I detected the problem:

model.py:

FOO_LIST = (('A', _('A msgid'),)

class FooModel(models.Model):
   a_list = models.StringField(choices=FOO_LIST)

django.po:

msgid "A msgid"
msgstr "Nön-ÁSCII chäráctèrs"

view.py:

FooForm = forms.form_for_model(FooModel)
f = FooForm()
return render_to_response('footemplate.html', {'f': f})

footemplate.html:

...
{{ f }}
...

Attachments

newforms_widget_smart_unicode.patch (1.8 kB) - added by framos@yaco.es on 05/14/07 06:45:44.
That works for me!

Change History

05/14/07 06:45:44 changed by framos@yaco.es

  • attachment newforms_widget_smart_unicode.patch added.

That works for me!

05/14/07 06:49:54 changed by mtredinnick

  • keywords changed from unicode gettext widget to unicode gettext widget unicode-branch.
  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

This is one of the key problems we are fixing in the unicode branch. Since there isn't a ticket open for this at the moment, I'll leave this open so that if anybody else repeats it, we have something to point them at..

05/30/07 13:55:13 changed by gustavo@fundacaoaprender.org.br

With LANGUAGE_CODE="pt-br", using the DateSelectWidget? from \newforms\widgets\extras\widgets.py, on revision 5381, I get an UnicodeDecodeError? on \newforms\widgets.py line 173.

Exception Type: UnicodeDecodeError? Exception Value: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128) Exception Location: D:\Python\lib\site-packages\django\newforms\widgets.py in render, line 173

173. output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(smart_unicode(option_label))))

Changing from "escape(smart_unicode(option_label))" to "smart_unicode(escape(option_label))" solved the problem for me too.

07/04/07 07:11:05 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5609]) Merged Unicode branch into trunk (r4952:5608). This should be fully backwards compatible for all practical purposes.

Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702


Add/Change #4291 (Unicode bug with gettext variables in newforms/widgets.py)




Change Properties
Action