#12849: django's development server raises an encoding exception when trying to
colorize non-ascii text
------------------------------------------------+---------------------------
Reporter: jype | Owner: nobody
Status: new | Milestone: 1.2
Component: django-admin.py runserver | Version: SVN
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
------------------------------------------------+---------------------------
Comment (by frasern):
I've created a patch (including tests) which allows unicode strings to be
passed to `colorize`.
I had started out with the intention of using
`django.utils.encoding.DEFAULT_LOCALE_ENCODING` (derived from
`locale.getdefaultlocale`); however, I think it is more correct to use
`locale.getpreferredencoding` and ended up going with that instead (in
case the user has somehow overridden the default).
For reference, the preferred encoding can be quickly and temporarily
altered by setting the `LC_CTYPE` environment variable:
{{{
#!html
<pre class="wiki">$ python
>>> import locale
>>> locale.getpreferredencoding()
'UTF-8'
>>> from django.utils.termcolors import colorize
>>> colorize(u'\u00a3', fg='red')
'\x1b[31m<strong style="text-
decoration:underline">\xc2\xa3</strong>\x1b[0m'</pre>
}}}
{{{
#!html
<pre class="wiki">$ LC_CTYPE=en_GB.iso-8859-1 python
>>> import locale
>>> locale.getpreferredencoding()
'ISO-8859-1'
>>> from django.utils.termcolors import colorize
>>> colorize(u'\u00a3', fg='red')
'\x1b[31m<strong style="text-
decoration:underline">\xa3</strong>\x1b[0m'</pre>
}}}
The function will still raise a `UnicodeEncodeError` if the unicode string
cannot be encoded to the preferred encoding, for example:
{{{
$ LC_CTYPE=en_GB.iso-8859-1 python
>>> from django.utils.termcolors import colorize
>>> colorize(u'\u0160', fg='red')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "django/utils/termcolors.py", line 49, in colorize
text = smart_str(text, encoding=encoding)
File "django/utils/encoding.py", line 117, in smart_str
return s.encode(encoding, errors)
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0160' in
position 0: ordinal not in range(256)
}}}
Note that the terminal will need to be set to use the same encoding in
order for the colorized output to be displayed correctly. PuTTY, for
example, has a
[http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter4.html#config-
charset configuration setting] for this.
--
Ticket URL: <http://code.djangoproject.com/ticket/12849#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.