Skip to content

Commit

Permalink
Stop using the deprecated locale.getdefaultlocale() function
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
  • Loading branch information
s0undt3ch committed Jun 29, 2023
1 parent 9aeed74 commit c463c94
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 89 deletions.
1 change: 0 additions & 1 deletion changelog/64457.changed.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 8 additions & 43 deletions salt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,6 @@
)


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

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions salt/client/ssh/ssh_py_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import datetime
import hashlib
import locale
import logging
import os
import platform
Expand Down Expand Up @@ -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__
Expand Down
37 changes: 0 additions & 37 deletions salt/utils/locales.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
the locale utils used by salt
"""
import locale
import sys

from salt.utils.decorators import memoize as real_memoize
Expand Down Expand Up @@ -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)

0 comments on commit c463c94

Please sign in to comment.