[Python-Dev] test_sys failures

Jeroen Ruigrok van der Werven asmodai at in-nomine.org
Tue Jan 8 12:46:09 CET 2008


-On [20080108 12:09], Nick Coghlan (ncoghlan at gmail.com) wrote:
>Redirecting stdout also fails for both the trunk and the py3k branch for 
>me on Ubuntu. If I redirected stderr as well then the tests worked again.
>
>Given that a pipe/file and the console very likely *do* have different 
>encodings, maybe the test is just wrong?

This sounds like a problem I recently blogged about (verbatim copy):

When you use Python with sys.stdout you might run into a problem where
sys.stdout.encoding suddenly becomes None. This happens due to the fact that
upon using a pipe or redirection, at least under Unix, it falls back to not
knowing anything about the target. In order to work around this you can add a
fallback to use locale.getpreferredencoding(). So if you use encode() on a
string you can do something like:

from locale import getpreferredencoding
 
text = u"Something special"
 
print text.encode(sys.stdout.encoding or getpreferredencoding() or 'ascii', 'replace')

This is how we currently use it within Babel as well for printing the locale
list.

-- 
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
Angel to some, Daemon to others...


More information about the Python-Dev mailing list