Skip to content

packaging: Remove locale fallbacks for Python older than 3.11 - #7697

Merged
wenzeslaus merged 1 commit into
OSGeo:mainfrom
wenzeslaus:locale-python-311-cleanup
Jul 14, 2026
Merged

wenzeslaus merged 1 commit into
OSGeo:mainfrom
wenzeslaus:locale-python-311-cleanup

Conversation

@wenzeslaus

Copy link
Copy Markdown
Member

Remove fallback for pre-Python 3.11 and use exclusively locale.getencoding(). While this is changing code, this is technically a further packaging-related follow-up to #7684, which raised the minimal Python version to 3.11 but missed these places. They are marked with a # Python >= 3.11 comment and handled through try-except ("ask for forgiveness") rather than branched with a sys.version_info check (sort of "ask for permission").

locale.getencoding() was added in Python 3.11, so the fallbacks to locale.getdefaultlocale() are never used now that 3.11 is the minimal supported version. On 3.11 and newer, locale.getencoding() is what already ran, so there is no change in behavior. As a bonus, locale.getdefaultlocale() is actually deprecated since 3.11, so this also removes the uses of it.

Two of the places call locale.getlocale() instead. That function is available in all supported versions and never raises AttributeError, so those fallbacks could not be reached even before this change. The (here unused) fallbacks are now removed.

Verified partially by AI agent by running the three _get_encoding() functions (grass.script.utils, grass.gunittest.multirunner, utils/mkhtml.py) and the two expressions in lib/init/grass.py (GUI places assumed to work as well).

AI (Claude Code with Opus) was used to find these places and to prepare this change based on the review for #7684.

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.
@github-actions github-actions Bot added GUI wxGUI related Python Related code is in Python libraries labels Jul 14, 2026

@echoix echoix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting to note, https://docs.python.org/3/library/os.html#utf8-mode will be the default in 3.15. The locale.getencoding() ignores this, while locale.getpreferredencoding(do_setlocale=True) respects it. The latter can avoid setting the locale with the flag.

This also brings the discussion: Should we always be writing out files as utf-8? In non-gui, we started specifying the encoding, and when its possible to circle in the producers and consumers of a function that wrote a file in python, we were normalizing it to UTF-8.
Does this align with what GUI does, and will be doing?

Anyways, everything else is fine as written, I'll approve after discussing this

@wenzeslaus
wenzeslaus merged commit 7a67a80 into OSGeo:main Jul 14, 2026
27 checks passed
@wenzeslaus
wenzeslaus deleted the locale-python-311-cleanup branch July 14, 2026 15:45
@wenzeslaus

Copy link
Copy Markdown
Member Author

All internal files (in the project), ASCII exports, or workspaces should be UTF-8 - that's the design. If it is reality, I can't tell from top of my head. I merged because there is no use in holding this open for that discussion.

echoix pushed a commit to wenzeslaus/grass that referenced this pull request Jul 14, 2026
…7697)

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.
@github-actions github-actions Bot added this to the 8.6.0 milestone Jul 14, 2026
@lindakarlovska

lindakarlovska commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

After this PR was merged, I struggled a bit with launching my built GRASS. Maybe someone else will find this helpful, so I am putting the solution here.

I have Ubuntu 22.04.5 LTS with the system Python 3.10 (python3), and I do not plan to upgrade my Ubuntu installation at the moment.

When I built GRASS, everything completed successfully, but when I ran it, I got this error:

`Launching <wxpython> GUI in the background, please wait... GRASS WebMercator/contours:grass > g.gui Launching <wxpython> GUI in the background, please wait... Traceback (most recent call last): File "/home/linduska/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/wxgui.py", line 30, in <module> from core.utils import registerPid, unregisterPid File "/home/linduska/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/core/utils.py", line 34, in <module> from core.gcmd import RunCommand File "/home/linduska/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/core/gcmd.py", line 820, in <module> _enc: str = GetDefaultEncoding() # define as global variable File "/home/linduska/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/core/gcmd.py", line 809, in GetDefaultEncoding enc = locale.getencoding() AttributeError: module 'locale' has no attribute 'getencoding'`

Makes sense since the min version is 3.11 now.

So I did the following:

  1. Install python3.11 and related utils:
sudo apt-get install -y python3.11
sudo apt install python3.11-venv python3.11-distutils
  1. I set up the virtual env:

python3.11 -m venv ~/venvs/grass311
source ~/venvs/grass311/bin/activate
  1. install Linux wxpython wheel
(grass311) linduska@linduska-470:~/grass$ pip install -U \
    -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-22.04 \
    wxPython

(pip install wxPython does not work for Linux)

  1. Ran GRASS again:

(grass311) linduska@linduska-470:~/grass$ ./bin.x86_64-pc-linux-gnu/grass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GUI wxGUI related libraries Python Related code is in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants