missing sys.setappdefaultencoding

Alex Martelli aleaxit at yahoo.com
Fri Jan 7 06:06:53 EST 2005


Uwe Mayer <merkosh at hadiko.de> wrote:

> Hi,
> 
> well, I wrote a nice python program which won't work if the default encoding
> has not been set from ascii to latin-1 or latin-15.

Then your program is not very nice...;-)

> However, the command sys.setappdefaultencoding is missing on a Python
> installation with Python 2.3.4 on Gentoo where it is present on Debian.
> 
> Any ideas how to deal with this?

The site.py module in the standard Python library deliberately REMOVES
the setdefaultencoding entry from the sys module (with a del statement)
just before it finishes initializing your site-specific environment.

This is meant as a very strong indication that you SHOULDN'T rely on
setdefaultencoding in your applications.  Being more explicit about the
codings to/from Unicode is way, way better.  I strongly suggest you only
use Unicode *within* your programs and transcode only at I/O time (if
needed: some good GUI toolkits, including Tkinter which comes with
Python by default, use Unocide too).  On the activestate's cookbook site
you'll find a nice recipe by your compatriot Holger Krekel pointing out
the practical course to follow, IMHO (I expand on that in the version of
this and other recipes I've edited for the forthcoming 2nd printed
edition of the Python Cookbook, but Holger's online recipe already has
the salient practical points you'd be well advised to follow).

If nevertheless you want to subvert the design intent of Python, Python
does give you enough rope to shoot yourself in the foot:
    import sys
    reload(sys)
and now, after reloading, sys.setdefaultencoding will be there anew.


Alex



More information about the Python-list mailing list