diff --git a/Lib/_bootlocale.py b/Lib/_bootlocale.py --- a/Lib/_bootlocale.py +++ b/Lib/_bootlocale.py @@ -9,21 +9,29 @@ import _locale if sys.platform.startswith("win"): def getpreferredencoding(do_setlocale=True): return _locale._getdefaultlocale()[1] else: try: _locale.CODESET except AttributeError: - def getpreferredencoding(do_setlocale=True): - # This path for legacy systems needs the more complex - # getdefaultlocale() function, import the full locale module. - import locale - return locale.getpreferredencoding(do_setlocale) + import sysconfig + if sysconfig.get_config_var('ANDROID_API_LEVEL'): + # On Android langinfo.h and CODESET is missing, and UTF-8 is always + # used in mbstowcs and wcstombs + def getpreferredencoding(do_setlocale=True): + assert not do_setlocale + return 'UTF-8' + else: + def getpreferredencoding(do_setlocale=True): + # This path for legacy systems needs the more complex + # getdefaultlocale() function, import the full locale module. + import locale + return locale.getpreferredencoding(do_setlocale) else: def getpreferredencoding(do_setlocale=True): assert not do_setlocale result = _locale.nl_langinfo(_locale.CODESET) if not result and sys.platform == 'darwin': # nl_langinfo can return an empty string # when the setting has an invalid value. # Default to UTF-8 in that case because diff --git a/Lib/locale.py b/Lib/locale.py --- a/Lib/locale.py +++ b/Lib/locale.py @@ -613,25 +613,31 @@ if sys.platform.startswith("win"): """Return the charset that the user is likely using.""" import _bootlocale return _bootlocale.getpreferredencoding(False) else: # On Unix, if CODESET is available, use that. try: CODESET except NameError: - # Fall back to parsing environment variables :-( - def getpreferredencoding(do_setlocale = True): - """Return the charset that the user is likely using, - by looking at environment variables.""" - res = getdefaultlocale()[1] - if res is None: - # LANG not set, default conservatively to ASCII - res = 'ascii' - return res + # Android always uses UTF-8 + import sysconfig + if sysconfig.get_config_var('ANDROID_API_LEVEL'): + def getpreferredencoding(do_setlocale = True): + return 'UTF-8' + else: + # Fall back to parsing environment variables :-( + def getpreferredencoding(do_setlocale = True): + """Return the charset that the user is likely using, + by looking at environment variables.""" + res = getdefaultlocale()[1] + if res is None: + # LANG not set, default conservatively to ASCII + res = 'ascii' + return res else: def getpreferredencoding(do_setlocale = True): """Return the charset that the user is likely using, according to the system configuration.""" import _bootlocale if do_setlocale: oldloc = setlocale(LC_CTYPE) try: