[GRASS-dev] locale problem solved - but have suggestion

Glynn Clements glynn at gclements.plus.com
Mon Jul 30 04:01:38 PDT 2012


Michael Barton wrote:

> After a lot of digging I finally traced the source of the locale
> problem that disables the menus and a related problem that throws a
> lot of errors about unknown locale into the terminal.
> 
> The serious error is in python/grass/scripts/task.py line 460, and can
> be fixed with an error trap as follows:
> 
>     try:
>         enc = locale.getdefaultlocale()[1]
>     except:
>         enc = None
> 
> 
> The more general problem, however, is the use of Python's
> locale.getdefaultlocale() in several places. This uses system locale
> variables, starting with LC_CTYPE. If one of these is unrecognizable
> by Python for some reason, it throws an error which may just put an
> error message in the terminal or could cause more serious problems
> like the one's I've been encountering on non-English systems. This
> apparently has happened variously on Mac's at least.

It's not Python which reads the environment variables; it's libc,
specifically setlocale().

If the environment variables are invalid, it will affect any library
functions which Python uses, along with any child processes spawned
from Python.

E.g. any localised text generated by GRASS modules will use the
encoding specified by the LC_CYTPE category, which is set from the
environment.

If you want to control the locale from the GUI, or from configuration
files, you have to set the environment variables correctly for any
child processes. However, setting the environment variables won't
affect Python's locale unless you call locale.setlocale() again. At
which point, locale.getlocale() should return the correct value.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list