diff --git a/changelog/64457.changed.md b/changelog/64457.changed.md index 3360eb9c0cc..8e3364743f9 100644 --- a/changelog/64457.changed.md +++ b/changelog/64457.changed.md @@ -3,5 +3,4 @@ Addressed Python 3.11 deprecations: * Switch to `FullArgSpec` since Py 3.11 no longer has `ArgSpec`, deprecated since Py 3.0 * Stopped using the deprecated `cgi` module. * Stopped using the deprecated `pipes` module -* Backport `locale.getdefaultlocale()` into Salt. It's getting removed in Py 3.13 * Stopped using the deprecated `imp` module diff --git a/salt/__init__.py b/salt/__init__.py index 32a73d568e7..4407a7d06f7 100644 --- a/salt/__init__.py +++ b/salt/__init__.py @@ -39,44 +39,6 @@ warnings.filterwarnings( ) -def __getdefaultlocale(envvars=("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE")): - """ - This function was backported from Py3.11 which started triggering a - deprecation warning about it's removal in 3.13. - """ - import locale - - try: - # check if it's supported by the _locale module - import _locale - - code, encoding = _locale._getdefaultlocale() - except (ImportError, AttributeError): - pass - else: - # make sure the code/encoding values are valid - if sys.platform == "win32" and code and code[:2] == "0x": - # map windows language identifier to language name - code = locale.windows_locale.get(int(code, 0)) - # ...add other platform-specific processing here, if - # necessary... - return code, encoding - - # fall back on POSIX behaviour - import os - - lookup = os.environ.get - for variable in envvars: - localename = lookup(variable, None) - if localename: - if variable == "LANGUAGE": - localename = localename.split(":")[0] - break - else: - localename = "C" - return locale._parse_localename(localename) - - def __define_global_system_encoding_variable__(): import sys @@ -95,13 +57,16 @@ def __define_global_system_encoding_variable__(): # If the system is properly configured this should return a valid # encoding. MS Windows has problems with this and reports the wrong # encoding + import locale try: - encoding = __getdefaultlocale()[-1] - except ValueError: - # A bad locale setting was most likely found: - # https://github.com/saltstack/salt/issues/26063 - pass + encoding = locale.getencoding() + except AttributeError: + # Python < 3.11 + encoding = locale.getpreferredencoding(do_setlocale=True) + + # This is now garbage collectable + del locale if not encoding: # This is most likely ascii which is not the best but we were diff --git a/salt/client/ssh/ssh_py_shim.py b/salt/client/ssh/ssh_py_shim.py index b77749f4953..9b8f9e0f658 100644 --- a/salt/client/ssh/ssh_py_shim.py +++ b/salt/client/ssh/ssh_py_shim.py @@ -67,14 +67,14 @@ def get_system_encoding(): import locale try: - encoding = locale.getdefaultlocale()[-1] - except ValueError: - # A bad locale setting was most likely found: - # https://github.com/saltstack/salt/issues/26063 - pass + encoding = locale.getencoding() + except AttributeError: + # Python < 3.11 + encoding = locale.getpreferredencoding(do_setlocale=True) # This is now garbage collectable del locale + if not encoding: # This is most likely ascii which is not the best but we were # unable to find a better encoding. If this fails, we fall all diff --git a/salt/grains/core.py b/salt/grains/core.py index afaa5389e39..920194be1ee 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -11,6 +11,7 @@ as those returned here import datetime import hashlib +import locale import logging import os import platform @@ -2688,10 +2689,8 @@ def locale_info(): ( grains["locale_info"]["defaultlanguage"], grains["locale_info"]["defaultencoding"], - ) = salt.utils.locales.getdefaultlocale() + ) = locale.getlocale() except Exception: # pylint: disable=broad-except - # locale.getdefaultlocale can ValueError!! Catch anything else it - # might do, per #2205 grains["locale_info"]["defaultlanguage"] = "unknown" grains["locale_info"]["defaultencoding"] = "unknown" grains["locale_info"]["detectedencoding"] = __salt_system_encoding__ diff --git a/salt/utils/locales.py b/salt/utils/locales.py index a380ddbe7a2..43c0f1c68d7 100644 --- a/salt/utils/locales.py +++ b/salt/utils/locales.py @@ -1,7 +1,6 @@ """ the locale utils used by salt """ -import locale import sys from salt.utils.decorators import memoize as real_memoize @@ -82,39 +81,3 @@ def normalize_locale(loc): comps["codeset"] = comps["codeset"].lower().replace("-", "") comps["charmap"] = "" return join_locale(comps) - - -def getdefaultlocale(envvars=("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE")): - """ - This function was backported from Py3.11 which started triggering a - deprecation warning about it's removal in 3.13. - """ - try: - # check if it's supported by the _locale module - import _locale - - code, encoding = _locale._getdefaultlocale() - except (ImportError, AttributeError): - pass - else: - # make sure the code/encoding values are valid - if sys.platform == "win32" and code and code[:2] == "0x": - # map windows language identifier to language name - code = locale.windows_locale.get(int(code, 0)) - # ...add other platform-specific processing here, if - # necessary... - return code, encoding - - # fall back on POSIX behaviour - import os - - lookup = os.environ.get - for variable in envvars: - localename = lookup(variable, None) - if localename: - if variable == "LANGUAGE": - localename = localename.split(":")[0] - break - else: - localename = "C" - return locale._parse_localename(localename)