getdefaultencoding - how to change this?

Helmut Jarausch jarausch at skynet.be
Thu Jan 20 12:20:14 EST 2011


On Thu, 20 Jan 2011 10:46:37 -0600, Robert Kern wrote:

> 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

Thanks Robert,
probably I wasn't too clear about my issue.
I couldn't "print" any non-ascii character to my console although
my console has an en_US.iso88591 locale.

I didn't modfiy anything in Python's source code.
I noticed that my root account still had an ASCII locale.
Now I have changed it such that the root account has the same locale
as non-root accounts. Recompiling Python under such a root account
has rectified things. Now I can print non-ascii characters if they are
properly encoded.

Thanks,
Helmut.




-- 



More information about the Python-list mailing list