Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

missing sys.setappdefaultencoding

2 views
Skip to first unread message

Uwe Mayer

unread,
Jan 7, 2005, 5:49:25 AM1/7/05
to
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.

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?

Thanks in advance,
Ciao
Uwe

Alex Martelli

unread,
Jan 7, 2005, 6:06:53 AM1/7/05
to
Uwe Mayer <mer...@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

Craig Ringer

unread,
Jan 7, 2005, 11:11:34 AM1/7/05
to Alex Martelli, Python List
On Fri, 2005-01-07 at 19:06, Alex Martelli wrote:
> Uwe Mayer <mer...@hadiko.de> wrote:

> > 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...;-)

Agreed. I prefer to use explicit str.encode(), str.decode() and
unicode() calls where appropriate.

On a side note, PEP 263 handles the text encoding interpretation of
Python program source, and is well worth reading and following.

http://python.org/peps/pep-0263.html

--
Craig Ringer

0 new messages