File tree Expand file tree Collapse file tree 3 files changed +29
-14
lines changed Expand file tree Collapse file tree 3 files changed +29
-14
lines changed Original file line number Diff line number Diff line change @@ -14,11 +14,17 @@ def getpreferredencoding(do_setlocale=True):
14
14
try :
15
15
_locale .CODESET
16
16
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 )
22
28
else :
23
29
def getpreferredencoding (do_setlocale = True ):
24
30
assert not do_setlocale
Original file line number Diff line number Diff line change @@ -618,15 +618,21 @@ def getpreferredencoding(do_setlocale = True):
618
618
try :
619
619
CODESET
620
620
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
630
636
else :
631
637
def getpreferredencoding (do_setlocale = True ):
632
638
"""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
13
13
- Issue #18896: Python function can now have more than 255 parameters.
14
14
collections.namedtuple() now supports tuples with more than 255 elements.
15
15
16
+ - Issue #28596: The preferred encoding is UTF-8 on Android. Patch written by
17
+ Chi Hsuan Yen.
18
+
16
19
- Issue #26919: On Android, operating system data is now always encoded/decoded
17
20
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
18
21
os.fsencode() and os.fsdecode() which are already using UTF-8.
You can’t perform that action at this time.
0 commit comments