I have a code base that runs unchanged in Python 2.7 and 3.2+. But the doctests in the documentation rst files are giving me a headache. When I run them in Python2, I get UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128)
. If I add
.. testsetup:: *
from __future__ import unicode_literals
then I get a lot of errors like
Expected:
'something'
Got:
u'something'
Is there a way to have doctest containing unicode characters in the rst files that work unchanged in Python 2.7 and 3.2+?
Make sure you are using Python 3.3. It added the explicit
u'unicode literals'
-- i.e. withu
prefix again -- to ease the transition between Python 2 with unicode literals and Python 3. See http://docs.python.org/3/whatsnew/3.3.html#pep-414-explicit-unicode-literals