Reliably getting/setting the (current) encoding name?

Mike C. Fletcher mcfletch at
Fri Jan 31 18:35:30 CET 2003

Michael Hudson wrote:

>"Mike C. Fletcher" <mcfletch at> writes:
>>The codecs module doesn't appear to have a "get default codec name"
>>function.  So I'm asking the question:
>>    How does one reliably get the currently-active default codec name
>>    (i.e. the one that reflects the currently-set locale) as suitable
>>    for use with uncode.encode( ... )?
>I don't know the answer to this one, I'm afraid.
 >>> import sys
 >>> sys.getdefaultencoding()

(Alex mentioned it in his post)


>Here I'd say: don't add conversion code "all over the joint", add it
>"at the edges", i.e. where the unicode strings get into your library.

Yes, I'm not particularly infatuated with the idea of multi-line 
conversion hacks all over my libraries :) , the difficulty with using 
the "edges" is that the (GUI) library is constantly interacting with 
various wxTextCtrl objects, which, in the Unicode builds of wxPython are 
suddenly returning unicode instead of str objects, which means the 
Unicode strings are arriving all through the library, sometimes being 
directed at the object model, sometimes at status messages, sometimes to 
the logging system etc.  At the moment, I'm going to go down the path to 
evil and requiring the application to set a useful default encoding 
using the hack Alex pointed out.


  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list