Unicode again ... default codec ...

Lele Gaifax lele at metapensiero.it
Wed Oct 21 05:24:55 EDT 2009


"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> writes:

> DON'T do that. Really. Changing the default encoding is a horrible,
> horrible hack and causes a lot of problems.
> ...
> More reasons:
> http://tarekziade.wordpress.com/2008/01/08/syssetdefaultencoding-is-evil/
> See also this recent thread in python-dev:
> http://comments.gmane.org/gmane.comp.python.devel/106134

This is a problem that appears quite often, against which I have yet to
see a general workaround, or even a "safe pattern". I must confess that
most often I just give up and change the "if 0:" line in
sitecustomize.py to enable a reasonable default...

A week ago I met another incarnation of the problem that I finally
solved by reloading the sys module, a very ugly way, don't tell me, and
I really would like to know a better way of doing it.

The case is simple enough: a unit test started failing miserably, with a
really strange traceback, and a quick pdb session revealed that the
culprit was nosetest, when it prints out the name of the test, using
some variant of "print testfunc.__doc__": since the latter happened to
be a unicode string containing some accented letters, that piece of
nosetest's code raised an encoding error, that went untrapped...

I tried to understand the issue, until I found that I was inside a fresh
new virtualenv with python 2.6 and the sitecustomize wasn't even
there. So, even if my shell environ was UTF-8 (the system being a Ubuntu
Jaunty), within that virtualenv Python's stdout encoding was
'ascii'. Rightly so, nosetest failed to encode the accented letters to
that.

I could just rephrase the test __doc__, or remove it, but to avoid
future noise I decided to go with the deprecated "reload(sys)" trick,
done as early as possible... damn, it's just a test suite after all!

Is there a "correct" way of dealing with this? What should nosetest
eventually do to initialize it's sys.output.encoding reflecting the
system's settings? And on the user side, how could I otherwise fix it (I
mean, without resorting to the reload())?

Thank you,
ciao, lele.
-- 
nickname: Lele Gaifax    | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas    | comincerò ad aver paura di chi mi copia.
lele at nautilus.homeip.net |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list