Repository navigation
packaging: Remove locale fallbacks for Python older than 3.11 - #7697
Conversation
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.
echoix
left a comment
There was a problem hiding this comment.
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
|
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. |
…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.
|
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: Makes sense since the min version is 3.11 now. So I did the following:
(pip install wxPython does not work for Linux)
|
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.11comment and handled through try-except ("ask for forgiveness") rather than branched with asys.version_infocheck (sort of "ask for permission").locale.getencoding()was added in Python 3.11, so the fallbacks tolocale.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 raisesAttributeError, 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 inlib/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.