Skip to content

Commit 6c9dcda

Browse files
author
Xavier de Gaye
committed
Issue #28596: The preferred encoding is UTF-8 on Android.
1 parent b06cde6 commit 6c9dcda

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

Lib/_bootlocale.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ def getpreferredencoding(do_setlocale=True):
1414
try:
1515
_locale.CODESET
1616
except AttributeError:
17-
def getpreferredencoding(do_setlocale=True):
18-
# This path for legacy systems needs the more complex
19-
# getdefaultlocale() function, import the full locale module.
20-
import locale
21-
return locale.getpreferredencoding(do_setlocale)
17+
if hasattr(sys, 'getandroidapilevel'):
18+
# On Android langinfo.h and CODESET are missing, and UTF-8 is
19+
# always used in mbstowcs() and wcstombs().
20+
def getpreferredencoding(do_setlocale=True):
21+
return 'UTF-8'
22+
else:
23+
def getpreferredencoding(do_setlocale=True):
24+
# This path for legacy systems needs the more complex
25+
# getdefaultlocale() function, import the full locale module.
26+
import locale
27+
return locale.getpreferredencoding(do_setlocale)
2228
else:
2329
def getpreferredencoding(do_setlocale=True):
2430
assert not do_setlocale

Lib/locale.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -618,15 +618,21 @@ def getpreferredencoding(do_setlocale = True):
618618
try:
619619
CODESET
620620
except NameError:
621-
# Fall back to parsing environment variables :-(
622-
def getpreferredencoding(do_setlocale = True):
623-
"""Return the charset that the user is likely using,
624-
by looking at environment variables."""
625-
res = getdefaultlocale()[1]
626-
if res is None:
627-
# LANG not set, default conservatively to ASCII
628-
res = 'ascii'
629-
return res
621+
if hasattr(sys, 'getandroidapilevel'):
622+
# On Android langinfo.h and CODESET are missing, and UTF-8 is
623+
# always used in mbstowcs() and wcstombs().
624+
def getpreferredencoding(do_setlocale = True):
625+
return 'UTF-8'
626+
else:
627+
# Fall back to parsing environment variables :-(
628+
def getpreferredencoding(do_setlocale = True):
629+
"""Return the charset that the user is likely using,
630+
by looking at environment variables."""
631+
res = getdefaultlocale()[1]
632+
if res is None:
633+
# LANG not set, default conservatively to ASCII
634+
res = 'ascii'
635+
return res
630636
else:
631637
def getpreferredencoding(do_setlocale = True):
632638
"""Return the charset that the user is likely using,

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
- Issue #18896: Python function can now have more than 255 parameters.
1414
collections.namedtuple() now supports tuples with more than 255 elements.
1515

16+
- Issue #28596: The preferred encoding is UTF-8 on Android. Patch written by
17+
Chi Hsuan Yen.
18+
1619
- Issue #26919: On Android, operating system data is now always encoded/decoded
1720
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
1821
os.fsencode() and os.fsdecode() which are already using UTF-8.

0 commit comments

Comments
 (0)