Stop using the deprecated locale.getdefaultlocale() function

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
Pedro Algarvio 2023-06-28 11:57:23 +01:00 committed by Pedro Algarvio
parent 9aeed74e96
commit c463c94b8d
5 changed files with 15 additions and 89 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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__

View file

@ -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)