[Tutor] locale.set/getlocale asymmetry

Albert-Jan Roskam fomcl at yahoo.com
Wed Feb 20 14:14:15 CET 2013

----- Original Message -----
> From: eryksun <eryksun at gmail.com>
> To: Albert-Jan Roskam <fomcl at yahoo.com>
> Cc: Python Mailing List <tutor at python.org>
> Sent: Wednesday, February 20, 2013 1:37 PM
> Subject: Re: [Tutor] locale.set/getlocale asymmetry
> On Wed, Feb 20, 2013 at 5:53 AM, Albert-Jan Roskam <fomcl at yahoo.com> 
> wrote:
>> There is an asymmetry in getlocale and setlocale. What is returned by
>> getlocale cannot be used as argument for setlocale.
> getlocale executes localename = _setlocale(category), where _setlocale
> wraps the C runtime setlocale. Then it transforms the return string to
> a tuple using _parse_localename, which calls locale.normalize. The
> latter changes the language and encoding incorrectly on Windows.
> Instead, you could save the unparsed locale string: old_locale =
> setlocale(LC_ALL).

Hi Eryksun,

Thank you. So can I simply use the function below to replace locale.getlocale()?
Will this never cause problems? The thing I remember about setlocale is that 
"thou shalt never call setlocale anywhere else but in the beginning of a script".
Or am I wrong here? I am always a little reluctant to mess with the host locale.
def getlocale(category=locale.LC_ALL):
    return locale.setlocale(category)

More information about the Tutor mailing list