Reliably getting/setting the (current) encoding name?

Alex Martelli aleax at aleax.it
Fri Jan 31 05:10:41 EST 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()

if 0:
    # Enable to support locale aware default string encodings.
    import locale
    loc = locale.getdefaultlocale()
    if loc[1]:
        encoding = loc[1]

if 0:
    # 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.


Alex





More information about the Python-list mailing list