File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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,
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments