diff options
author | Victor Stinner <vstinner@python.org> | 2023-06-06 14:55:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-06 14:55:21 (GMT) |
commit | b1a91d26c67250ff7abeb20064e7766096604001 (patch) | |
tree | 8d93339fa3dd90aae408c5ff63b0a71fd409f64d /Lib | |
parent | 3a975b5e92736cd2f68a09aa71a17d373b9355e9 (diff) | |
download | cpython-b1a91d26c67250ff7abeb20064e7766096604001.zip cpython-b1a91d26c67250ff7abeb20064e7766096604001.tar.gz cpython-b1a91d26c67250ff7abeb20064e7766096604001.tar.bz2 |
gh-104783: locale.getencoding() fallback uses FS encoding (#105381)
The locale.getencoding() function now uses
sys.getfilesystemencoding() if _locale.getencoding() is missing,
instead of calling locale.getdefaultlocale().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/locale.py | 14 | ||||
-rw-r--r-- | Lib/test/test_locale.py | 11 |
2 files changed, 16 insertions, 9 deletions
diff --git a/Lib/locale.py b/Lib/locale.py index 0197967..cd52ecd 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -616,16 +616,12 @@ def setlocale(category, locale=None): 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 diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py index b0d7998..da4bd79 100644 --- a/Lib/test/test_locale.py +++ b/Lib/test/test_locale.py @@ -1,6 +1,8 @@ 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 @@ -523,6 +525,15 @@ class TestMiscellaneous(unittest.TestCase): # 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() |