From 8921ed590f44dc8cafa861729aea4abdfe635d08 Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Tue, 14 Jul 2026 08:45:06 -0400 Subject: [PATCH] packaging: Remove locale fallbacks for Python older than 3.11 locale.getencoding() was added in Python 3.11, so the fallbacks to the deprecated locale.getdefaultlocale() are never used now that 3.11 is the minimal supported version. Call locale.getencoding() directly. The two places calling locale.getlocale() get the same treatment. That function is available in all supported versions, so those fallbacks could not be reached even before. --- gui/wxpython/core/gcmd.py | 6 +----- gui/wxpython/core/gconsole.py | 6 +----- gui/wxpython/gui_core/forms.py | 18 +++--------------- gui/wxpython/gui_core/ghelp.py | 6 +----- gui/wxpython/rlisetup/frame.py | 6 +----- lib/init/grass.py | 12 ++---------- python/grass/gunittest/multirunner.py | 6 +----- python/grass/script/utils.py | 6 +----- utils/mkhtml.py | 6 +----- 9 files changed, 12 insertions(+), 60 deletions(-) diff --git a/gui/wxpython/core/gcmd.py b/gui/wxpython/core/gcmd.py index 7db9392cf3c..0d6c53a4d5f 100644 --- a/gui/wxpython/core/gcmd.py +++ b/gui/wxpython/core/gcmd.py @@ -806,11 +806,7 @@ def GetDefaultEncoding(forceUTF8: bool = False) -> str: :return: system encoding (can be None) """ - try: - # Python >= 3.11 - enc = locale.getencoding() - except AttributeError: - enc = locale.getdefaultlocale()[1] + enc = locale.getencoding() if forceUTF8 and (enc is None or enc == "UTF8"): return "UTF-8" diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index 26a46e8b54a..048af870544 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -410,11 +410,7 @@ def Redirect(self): sys.stdout = self.cmdStdOut sys.stderr = self.cmdStdErr else: - try: - # Python >= 3.11 - enc = locale.getencoding() - except AttributeError: - enc = locale.getdefaultlocale()[1] + enc = locale.getencoding() if enc: # https://stackoverflow.com/questions/4374455/how-to-set-sys-stdout-encoding-in-python-3 sys.stdout = codecs.getwriter(enc)(sys.__stdout__.detach()) diff --git a/gui/wxpython/gui_core/forms.py b/gui/wxpython/gui_core/forms.py index b88b9fb04f0..9c45336aa3b 100644 --- a/gui/wxpython/gui_core/forms.py +++ b/gui/wxpython/gui_core/forms.py @@ -2430,11 +2430,7 @@ def __init__(self, parent, giface, task, id=wx.ID_ANY, frame=None, *args, **kwar ) if p.get("value", "") and Path(p["value"]).is_file(): ifbb.Clear() - try: - # Python >= 3.11 - enc = locale.getencoding() - except AttributeError: - enc = locale.getdefaultlocale()[1] + enc = locale.getencoding() with codecs.open( p["value"], encoding=enc, errors="ignore" ) as f: @@ -2991,11 +2987,7 @@ def OnFileSave(self, event): if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() - try: - # Python >= 3.11 - enc = locale.getencoding() - except AttributeError: - enc = locale.getdefaultlocale()[1] + enc = locale.getencoding() with codecs.open(path, encoding=enc, mode="w", errors="replace") as f: f.write(text + os.linesep) @@ -3017,11 +3009,7 @@ def OnFileText(self, event): filename = grass.tempfile() win.SetValue(filename) - try: - # Python >= 3.11 - enc = locale.getencoding() - except AttributeError: - enc = locale.getdefaultlocale()[1] + enc = locale.getencoding() with codecs.open(filename, encoding=enc, mode="w", errors="replace") as f: f.write(text) diff --git a/gui/wxpython/gui_core/ghelp.py b/gui/wxpython/gui_core/ghelp.py index 6ab7e000909..2e5f516d300 100644 --- a/gui/wxpython/gui_core/ghelp.py +++ b/gui/wxpython/gui_core/ghelp.py @@ -254,11 +254,7 @@ def _pageInfo(self): if not self.langUsed: import locale - try: - # Python >= 3.11 - loc = locale.getlocale() - except AttributeError: - loc = locale.getdefaultlocale() + loc = locale.getlocale() if loc == (None, None): self.langUsed = _("unknown") else: diff --git a/gui/wxpython/rlisetup/frame.py b/gui/wxpython/rlisetup/frame.py index e8b5cfc2cbe..29368ad8693 100644 --- a/gui/wxpython/rlisetup/frame.py +++ b/gui/wxpython/rlisetup/frame.py @@ -57,11 +57,7 @@ def __init__( self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose) self.btn_ok.Bind(wx.EVT_BUTTON, self.OnOk) self._layout() - try: - # Python >= 3.11 - self.enc = locale.getencoding() - except AttributeError: - self.enc = locale.getdefaultlocale()[1] + self.enc = locale.getencoding() def _layout(self): """Set the layout""" diff --git a/lib/init/grass.py b/lib/init/grass.py index 21605f0268c..caea0b32f11 100755 --- a/lib/init/grass.py +++ b/lib/init/grass.py @@ -72,11 +72,7 @@ GISBASE = None -try: - # Python >= 3.11 - ENCODING = locale.getencoding() -except AttributeError: - ENCODING = locale.getdefaultlocale()[1] +ENCODING = locale.getencoding() if ENCODING is None: ENCODING = "UTF-8" print("Default locale not found, using UTF-8") # intentionally not translatable @@ -1097,11 +1093,7 @@ def set_language(grass_config_dir: StrPath) -> None: "Default locale settings are missing. GRASS running with C locale.\n" ) - try: - # Python >= 3.11 - language, encoding = locale.getlocale() - except AttributeError: - language, encoding = locale.getdefaultlocale() + language, encoding = locale.getlocale() if not language: sys.stderr.write( "Default locale settings are missing. GRASS running with C locale.\n" diff --git a/python/grass/gunittest/multirunner.py b/python/grass/gunittest/multirunner.py index 9bd6ed538ae..f6902abbe64 100644 --- a/python/grass/gunittest/multirunner.py +++ b/python/grass/gunittest/multirunner.py @@ -17,11 +17,7 @@ def _get_encoding(): - try: - # Python >= 3.11 - encoding = locale.getencoding() - except AttributeError: - encoding = locale.getdefaultlocale()[1] + encoding = locale.getencoding() if not encoding: encoding = "UTF-8" return encoding diff --git a/python/grass/script/utils.py b/python/grass/script/utils.py index 86cfb8108bc..f2538ce7df9 100644 --- a/python/grass/script/utils.py +++ b/python/grass/script/utils.py @@ -216,11 +216,7 @@ def __setattr__(self, key: str, value: VT) -> None: def _get_encoding() -> str: - try: - # Python >= 3.11 - encoding = locale.getencoding() - except AttributeError: - encoding = locale.getdefaultlocale()[1] + encoding = locale.getencoding() if not encoding: encoding = "UTF-8" return encoding diff --git a/utils/mkhtml.py b/utils/mkhtml.py index 461a5618bb1..e8e55bbedeb 100644 --- a/utils/mkhtml.py +++ b/utils/mkhtml.py @@ -71,11 +71,7 @@ def _get_encoding(): - try: - # Python >= 3.11 - encoding = locale.getencoding() - except AttributeError: - encoding = locale.getdefaultlocale()[1] + encoding = locale.getencoding() if not encoding: encoding = "UTF-8" return encoding