getdefaultencoding - how to change this?

Robert Kern robert.kern at gmail.com
Thu Jan 20 11:46:37 EST 2011


On 1/20/11 8:31 AM, Helmut Jarausch wrote:
> Hi,
> I've searched the net but didn't find the information I need.
> Using Python-2.7.1, I know, I can't modify defaultencoding at run time.

You're not supposed to. It must remain 'ascii'. Otherwise, you will break dict 
lookups among other things. Can you be specific about why you think you want to 
change this? What are you trying to achieve? It looks like you are conflating a 
variety of different behaviors below.

> Python even ignores
> export PYTHONIOENCODING=ISO8859-1
>
> locale.getdefaultlocale()[1]
> returns
> 'ISO8859-1'

This does not introspect the same thing as sys.getdefaultencoding().

> still sys.stdout is using the ascii codec.

This reflects your terminal settings, not sys.getdefaultencoding(). I'm not sure 
why the PYTHONIOENCODING variable isn't affecting that. It works fine for me.

> How can I recompile Python (itself) to change this to iso8859-1 ?
> (My favourite editor cannot be told to use unicode.)

You wouldn't want to change sys.getdefaultencoding() for this. That parameter 
affects how str and unicode objects are converted between each other without an 
explicit .encode()/.decode() call, not what encoding Python assumes for the 
parsing of the code.

Instead, you want to use an encoding declaration in each file:

http://docs.python.org/reference/lexical_analysis.html#encoding-declarations

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list