Skip to content

Commit

Permalink
avoid using deprecated locale.getdefaultlocale()
Browse files Browse the repository at this point in the history
* fix #2538
* Use getencoding() where only encoding is needed; getlocale() otherwise
  • Loading branch information
pesekon2 committed Dec 2, 2022
1 parent 5d2a930 commit 722aaa0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
6 changes: 5 additions & 1 deletion gui/wxpython/core/gcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,11 @@ def GetDefaultEncoding(forceUTF8=False):
:return: system encoding (can be None)
"""
enc = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
enc = locale.getencoding()
except AttributeError:
enc = locale.getdefaultlocale()[1]
if forceUTF8 and (enc is None or enc == "UTF8"):
return "UTF-8"

Expand Down
6 changes: 5 additions & 1 deletion gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ def Redirect(self):
sys.stdout = self.cmdStdOut
sys.stderr = self.cmdStdErr
else:
enc = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
enc = locale.getencoding()
except AttributeError:
enc = locale.getdefaultlocale()[1]
if enc:
if sys.version_info.major == 2:
sys.stdout = codecs.getwriter(enc)(sys.__stdout__)
Expand Down
18 changes: 15 additions & 3 deletions gui/wxpython/gui_core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,11 @@ def __init__(self, parent, giface, task, id=wx.ID_ANY, frame=None, *args, **kwar
)
if p.get("value", "") and os.path.isfile(p["value"]):
ifbb.Clear()
enc = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
enc = locale.getencoding()
except AttributeError:
enc = locale.getdefaultlocale()[1]
with codecs.open(
p["value"], encoding=enc, errors="ignore"
) as f:
Expand Down Expand Up @@ -2594,7 +2598,11 @@ def OnFileSave(self, event):

if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
enc = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
enc = locale.getencoding()
except AttributeError:
enc = locale.getdefaultlocale()[1]
f = codecs.open(path, encoding=enc, mode="w", errors="replace")
try:
f.write(text + os.linesep)
Expand All @@ -2618,7 +2626,11 @@ def OnFileText(self, event):
filename = grass.tempfile()
win.SetValue(filename)

enc = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
enc = locale.getencoding()
except AttributeError:
enc = locale.getdefaultlocale()[1]
f = codecs.open(filename, encoding=enc, mode="w", errors="replace")
try:
f.write(text)
Expand Down
6 changes: 5 additions & 1 deletion gui/wxpython/gui_core/ghelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ def _pageInfo(self):
if not self.langUsed:
import locale

loc = locale.getdefaultlocale()
try:
# Python >= 3.11
loc = locale.getlocale()
except AttributeError:
loc = locale.getdefaultlocale()
if loc == (None, None):
self.langUsed = _("unknown")
else:
Expand Down
6 changes: 5 additions & 1 deletion gui/wxpython/rlisetup/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ def __init__(
self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
self.btn_ok.Bind(wx.EVT_BUTTON, self.OnOk)
self._layout()
self.enc = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
self.enc = locale.getencoding()
except AttributeError:
self.enc = locale.getdefaultlocale()[1]

def _layout(self):
"""Set the layout"""
Expand Down
12 changes: 10 additions & 2 deletions lib/init/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@
# for wxpath
_WXPYTHON_BASE = None

ENCODING = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
ENCODING = locale.getencoding()
except AttributeError:
ENCODING = locale.getdefaultlocale()[1]
if ENCODING is None:
ENCODING = "UTF-8"
print("Default locale not found, using UTF-8") # intentionally not translatable
Expand Down Expand Up @@ -1414,7 +1418,11 @@ def set_language(grass_config_dir):
"Default locale settings are missing. GRASS running with C locale.\n"
)

language, encoding = locale.getdefaultlocale()
try:
# Python >= 3.11
language, encoding = locale.getlocale()
except AttributeError:
language, encoding = locale.getdefaultlocale()
if not language:
sys.stderr.write(
"Default locale settings are missing. GRASS running with C locale.\n"
Expand Down
6 changes: 5 additions & 1 deletion python/grass/gunittest/multirunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@


def _get_encoding():
encoding = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
encoding = locale.getencoding()
except AttributeError:
encoding = locale.getdefaultlocale()[1]
if not encoding:
encoding = "UTF-8"
return encoding
Expand Down
6 changes: 5 additions & 1 deletion python/grass/script/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ def __setattr__(self, key, value):


def _get_encoding():
encoding = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
encoding = locale.getencoding()
except AttributeError:
encoding = locale.getdefaultlocale()[1]
if not encoding:
encoding = "UTF-8"
return encoding
Expand Down
6 changes: 5 additions & 1 deletion utils/mkhtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@


def _get_encoding():
encoding = locale.getdefaultlocale()[1]
try:
# Python >= 3.11
encoding = locale.getencoding()
except AttributeError:
encoding = locale.getdefaultlocale()[1]
if not encoding:
encoding = "UTF-8"
return encoding
Expand Down

0 comments on commit 722aaa0

Please sign in to comment.