[issue20087] Mismatch between glibc and X11 locale.alias

Benjamin Peterson report at bugs.python.org
Fri Mar 10 02:37:04 EST 2017


Benjamin Peterson added the comment:

Do you believe this program should work?

import locale, os
for l in open("/usr/share/i18n/SUPPORTED"):
    alias, encoding = l.strip().split()
    locale.setlocale(locale.LC_ALL, alias)
    try:
        enc = locale.getlocale()[1]
    except ValueError:
        continue # not in table
    normalized = enc.replace("ISO", "ISO-"). \
                     replace("_", "-"). \
                     replace("euc", "EUC-"). \
                     replace("big5", "big5-").upper()
    assert normalized == locale.nl_langinfo(locale.CODESET)

After my change it does—the encoding returned from getlocale() is the one actually being used by glibc. It fails dramatically on earlier versions of Python (for example on the en_IN example from #29571.) I don't understand why Python needs to editorialize whatever choices libc or the system administrator has made.

Is getlocale() expected to return something different from the underlying C locale?

In fact, why have this table at all instead of using nl_langinfo to return the encoding for the current locale?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20087>
_______________________________________


More information about the Python-bugs-list mailing list