Unicode again ... default codec ...

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Oct 22 06:17:15 EDT 2009


En Thu, 22 Oct 2009 05:25:16 -0300, Lele Gaifax <lele at metapensiero.it>  
escribió:
> "Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> writes:
>
>> En Wed, 21 Oct 2009 06:24:55 -0300, Lele Gaifax <lele at metapensiero.it>
>> escribió:
>>
>>> "Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> writes:
>>>
>> nosetest should do nothing special. You should configure the environment
>> so Python *knows* that your console understands utf-8.

> This is the same on my virtualenv:
>
>     $ python -c "import sys; print sys.getdefaultencoding(),  
> sys.stdout.encoding"
>     ascii UTF-8
>     $ python -c "print u'\xe1\xf1\xe7'"
>     áñç

Good, so stdout's encoding isn't really the problem.

> But look at this:
>
>       File "/usr/lib/python2.6/unittest.py", line 730, in printErrorList
>         self.stream.writeln("%s: %s" %  
> (flavour,self.getDescription(test)))
>       File "/usr/lib/python2.6/unittest.py", line 665, in writeln
>         if arg: self.write(arg)
>     UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in  
> position 10: ordinal not in range(128)
>
> Who is the culprit here?

unittest, or ultimately, this bug: http://bugs.python.org/issue4947

This is not specific to nosetest; unittest in verbose mode fails in the  
same way.

fix: add this method to the _WritelnDecorator class in unittest.py (near  
line 664):

     def write(self, arg):
         if isinstance(arg, unicode):
             arg = arg.encode(self.stream.encoding, "replace")
         self.stream.write(arg)

> The fact is, encodings are the real Y2k problem, and they are here to
> stay for a while!

Ok, but the idea is to solve the problem (or not let it happen in the  
first place!), not hide it under the rug :)

-- 
Gabriel Genellina




More information about the Python-list mailing list