By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,267 Members | 1,800 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,267 IT Pros & Developers. It's quick & easy.

missing sys.setappdefaultencoding

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Uwe Mayer <me*****@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
Jul 18 '05 #2

P: n/a
On Fri, 2005-01-07 at 19:06, Alex Martelli wrote:
Uwe Mayer <me*****@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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.