summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier de Gaye <xdegaye@users.sourceforge.net>2016-12-17 08:19:11 (GMT)
committerXavier de Gaye <xdegaye@users.sourceforge.net>2016-12-17 08:19:11 (GMT)
commit6c9dcda6b4e58367128726afc41fe05ee2f6388d (patch)
tree7e0636952cccad165fdf4b741a65d0a4cf046de5
parentb06cde61a2105cf9e0ec52fd7e2c131ae0cd15fa (diff)
downloadcpython-6c9dcda6b4e58367128726afc41fe05ee2f6388d.zip
cpython-6c9dcda6b4e58367128726afc41fe05ee2f6388d.tar.gz
cpython-6c9dcda6b4e58367128726afc41fe05ee2f6388d.tar.bz2
Issue #28596: The preferred encoding is UTF-8 on Android.
-rw-r--r--Lib/_bootlocale.py16
-rw-r--r--Lib/locale.py24
-rw-r--r--Misc/NEWS3
3 files changed, 29 insertions, 14 deletions
diff --git a/Lib/_bootlocale.py b/Lib/_bootlocale.py
index 4bccac1..0c61b0d 100644
--- a/Lib/_bootlocale.py
+++ b/Lib/_bootlocale.py
@@ -14,11 +14,17 @@ 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)
+ if hasattr(sys, 'getandroidapilevel'):
+ # On Android langinfo.h and CODESET are missing, and UTF-8 is
+ # always used in mbstowcs() and wcstombs().
+ def getpreferredencoding(do_setlocale=True):
+ 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
diff --git a/Lib/locale.py b/Lib/locale.py
index 4de0090..f8d1d78 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -618,15 +618,21 @@ else:
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
+ if hasattr(sys, 'getandroidapilevel'):
+ # On Android langinfo.h and CODESET are missing, and UTF-8 is
+ # always used in mbstowcs() and wcstombs().
+ 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,
diff --git a/Misc/NEWS b/Misc/NEWS
index 3ef25b6..b2cbad1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
- Issue #18896: Python function can now have more than 255 parameters.
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
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
os.fsencode() and os.fsdecode() which are already using UTF-8.