File tree Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -616,16 +616,12 @@ def setlocale(category, locale=None):
616616try :
617617 from _locale import getencoding
618618except ImportError :
619+ # When _locale.getencoding() is missing, locale.getencoding() uses the
620+ # Python filesystem encoding.
621+ _encoding = sys .getfilesystemencoding ()
619622 def getencoding ():
620- if hasattr (sys , 'getandroidapilevel' ):
621- # On Android langinfo.h and CODESET are missing, and UTF-8 is
622- # always used in mbstowcs() and wcstombs().
623- return 'utf-8'
624- encoding = _getdefaultlocale ()[1 ]
625- if encoding is None :
626- # LANG not set, default to UTF-8
627- encoding = 'utf-8'
628- return encoding
623+ return _encoding
624+
629625
630626try :
631627 CODESET
Original file line number Diff line number Diff line change 11from decimal import Decimal
22from test .support import verbose , is_android , is_emscripten , is_wasi
33from test .support .warnings_helper import check_warnings
4+ from test .support .import_helper import import_fresh_module
5+ from unittest import mock
46import unittest
57import locale
68import sys
@@ -523,6 +525,15 @@ def test_getencoding(self):
523525 # make sure it is valid
524526 codecs .lookup (enc )
525527
528+ def test_getencoding_fallback (self ):
529+ # When _locale.getencoding() is missing, locale.getencoding() uses
530+ # the Python filesystem
531+ encoding = 'FALLBACK_ENCODING'
532+ with mock .patch .object (sys , 'getfilesystemencoding' ,
533+ return_value = encoding ):
534+ locale_fallback = import_fresh_module ('locale' , blocked = ['_locale' ])
535+ self .assertEqual (locale_fallback .getencoding (), encoding )
536+
526537 def test_getpreferredencoding (self ):
527538 # Invoke getpreferredencoding to make sure it does not cause exceptions.
528539 enc = locale .getpreferredencoding ()
You can’t perform that action at this time.
0 commit comments