The following is a statement that should raise an UnicodeEncodeError
exception:
print 'str+{}'.format(u'unicode:\u2019')
In a Python shell, the exception is raised as expected:
>>> print 'str+{}'.format(u'unicode:\u2019')
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
print 'str+{}'.format(u'unicode:\u2019')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 8: ordinal not in range(128)
However, if I place that line at the start of my settings.py
and start the Django server from Aptana Studio, no error is raised and this line is printed:
str+unicode:’
But if I execute manage.py runserver
from a shell, the exception is raised:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 8: ordinal not in range(128)
Is there some sort of Python setting that silently suppresses these unicode errors?
How can I prevent the unicode error from being ignored when I start the Django test server directly from Aptana Studio?
Using
- Python 2.7.3
- Aptana Studio 3.3.2
If you simply cast a bytestring to unicode, like
or mix unicode and bytestrings in string formatting operations like your example, Python will fall back on the system default encoding (which is
ascii
unless it has been changed), and implicitly will try to encode unicode / decode the bytestring using theascii
codec.The currently active system default encoding can be displayed with
Now it seems like Aptana Studio does in fact mess with your interpreters default encoding:
From a blog post by Mikko Ohtamaa:
So make sure you reset the default to
ascii
, and you should be good.