try:
from _locale import getencoding
except ImportError:
+ # When _locale.getencoding() is missing, locale.getencoding() uses the
+ # Python filesystem encoding.
+ _encoding = sys.getfilesystemencoding()
def getencoding():
- if hasattr(sys, 'getandroidapilevel'):
- # On Android langinfo.h and CODESET are missing, and UTF-8 is
- # always used in mbstowcs() and wcstombs().
- return 'utf-8'
- encoding = _getdefaultlocale()[1]
- if encoding is None:
- # LANG not set, default to UTF-8
- encoding = 'utf-8'
- return encoding
+ return _encoding
+
try:
CODESET
from decimal import Decimal
from test.support import verbose, is_android, is_emscripten, is_wasi
from test.support.warnings_helper import check_warnings
+from test.support.import_helper import import_fresh_module
+from unittest import mock
import unittest
import locale
import sys
# make sure it is valid
codecs.lookup(enc)
+ def test_getencoding_fallback(self):
+ # When _locale.getencoding() is missing, locale.getencoding() uses
+ # the Python filesystem
+ encoding = 'FALLBACK_ENCODING'
+ with mock.patch.object(sys, 'getfilesystemencoding',
+ return_value=encoding):
+ locale_fallback = import_fresh_module('locale', blocked=['_locale'])
+ self.assertEqual(locale_fallback.getencoding(), encoding)
+
def test_getpreferredencoding(self):
# Invoke getpreferredencoding to make sure it does not cause exceptions.
enc = locale.getpreferredencoding()