Issue #28596: The preferred encoding is UTF-8 on Android.

This commit is contained in:
Xavier de Gaye 2016-12-17 09:19:11 +01:00
parent b06cde61a2
commit 6c9dcda6b4
3 changed files with 29 additions and 14 deletions

View File

@ -14,11 +14,17 @@ def getpreferredencoding(do_setlocale=True):
try: try:
_locale.CODESET _locale.CODESET
except AttributeError: except AttributeError:
def getpreferredencoding(do_setlocale=True): if hasattr(sys, 'getandroidapilevel'):
# This path for legacy systems needs the more complex # On Android langinfo.h and CODESET are missing, and UTF-8 is
# getdefaultlocale() function, import the full locale module. # always used in mbstowcs() and wcstombs().
import locale def getpreferredencoding(do_setlocale=True):
return locale.getpreferredencoding(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: else:
def getpreferredencoding(do_setlocale=True): def getpreferredencoding(do_setlocale=True):
assert not do_setlocale assert not do_setlocale

View File

@ -618,15 +618,21 @@ def getpreferredencoding(do_setlocale = True):
try: try:
CODESET CODESET
except NameError: except NameError:
# Fall back to parsing environment variables :-( if hasattr(sys, 'getandroidapilevel'):
def getpreferredencoding(do_setlocale = True): # On Android langinfo.h and CODESET are missing, and UTF-8 is
"""Return the charset that the user is likely using, # always used in mbstowcs() and wcstombs().
by looking at environment variables.""" def getpreferredencoding(do_setlocale = True):
res = getdefaultlocale()[1] return 'UTF-8'
if res is None: else:
# LANG not set, default conservatively to ASCII # Fall back to parsing environment variables :-(
res = 'ascii' def getpreferredencoding(do_setlocale = True):
return res """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: else:
def getpreferredencoding(do_setlocale = True): def getpreferredencoding(do_setlocale = True):
"""Return the charset that the user is likely using, """Return the charset that the user is likely using,

View File

@ -13,6 +13,9 @@ Core and Builtins
- Issue #18896: Python function can now have more than 255 parameters. - Issue #18896: Python function can now have more than 255 parameters.
collections.namedtuple() now supports tuples with more than 255 elements. collections.namedtuple() now supports tuples with more than 255 elements.
- Issue #28596: The preferred encoding is UTF-8 on Android. Patch written by
Chi Hsuan Yen.
- Issue #26919: On Android, operating system data is now always encoded/decoded - Issue #26919: On Android, operating system data is now always encoded/decoded
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
os.fsencode() and os.fsdecode() which are already using UTF-8. os.fsencode() and os.fsdecode() which are already using UTF-8.