Reliably getting/setting the (current) encoding name?
aleax at aleax.it
Fri Jan 31 11:10:41 CET 2003
Mike C. Fletcher wrote:
> 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( ... )?
Here's how site.py (in your Python library) does it:
encoding = "ascii" # Default value set by _PyUnicode_Init()
# Enable to support locale aware default string encodings.
loc = locale.getdefaultlocale()
encoding = loc
# Enable to switch off string to Unicode coercion and implicit
# Unicode to string conversion.
encoding = "undefined"
if encoding != "ascii":
# On Non-Unicode builds this will raise an AttributeError...
sys.setdefaultencoding(encoding) # Needs Python Unicode build !
> How does one set the default encoding to be used by:
> str( unicode )
with sys.setdefaultencoding -- but site.py UNBINDS that name
right after trying to import sitecustomize -- the idea being
that setting the default encoding is a task for the site, NOT
for authors of libraries etc.
If you disagree, "reload(sys)" will let you get at
sys.setdefaultencoding at any time -- use at your own risk.
More information about the Python-list