classification
Title: locale.resetlocale is broken
Type: behavior Stage: needs patch
Components: Library (Lib) Versions: Python 3.4, Python 3.3, Python 2.7
process
Status: open Resolution:
Dependencies: 813449 Superseder:
Assigned To: Nosy List: georg.brandl, georg.brandl, jlgijsbers, lemburg, loewis, meonkeys, mhammond, syvere, tim.peters
Priority: normal Keywords:

Created on 2002-01-16 04:56 by syvere, last changed 2014-02-03 15:43 by BreamoreBoy.

Messages (11)
msg8774 - (view) Author: Syver Enstad (syvere) Date: 2002-01-16 04:56
locale.setlocale doesn't recognize the the format that 
locale.py uses to set the locale, ie. no_NO and 
friends.

The only way I've succeeded in setting the locale on 
Python 2.1 is to use the format described in the 
Visual C++ C-runtime library docs for setlocale where 
a more verbose format is used to set the locale.
msg8775 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2002-01-16 05:07
Logged In: YES 
user_id=31435

Mark, know anything about this?  I don't.
msg8776 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-01-16 11:50
Logged In: YES 
user_id=21627

Can you provide a detailed test case? AFAIK, no_NO is indeed
no supported locale name on Windows, and I don't think
Python aanywhere uses it without the application explicitly
saying so.
msg8777 - (view) Author: Syver Enstad (syvere) Date: 2002-01-16 13:39
Logged In: YES 
user_id=428736

Sorry, I forgot to mention the testcase I am using. The 
test case that fails is the locale module itself, when 
running it as a standalone application that is.

To be more specific:
  File "d:\devtools\python21\lib\locale.py", line 384, in 
resetlocale
    _setlocale(category, _build_localename(getdefaultlocale
()))
locale.Error: locale setting not supported

And to clarify what input getdefaultlocale returns on my 
machine:

>>> locale.getdefaultlocale()
('no_NO', 'cp1252')

and:
>>> locale._build_localename(locale.getdefaultlocale())
'no_NO.cp1252'

By the way, is this bug fixed in python 2.2?
msg8778 - (view) Author: Johannes Gijsbers (jlgijsbers) * Date: 2004-11-08 18:59
Logged In: YES 
user_id=469548

Still reproducible with Python 2.4:

Python 2.4b2 (#19, Nov  8 2004, 11:15:07)
[GCC 3.3.5 (Debian 1:3.3.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import locale
>>> locale.getdefaultlocale()
['en_US', 'utf']
>>> locale.resetlocale()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/home/johannes/python/Lib/locale.py", line 389, in
resetlocale
    _setlocale(category, _build_localename(getdefaultlocale()))
locale.Error: unsupported locale setting

Note that if I run python with 'LANG=nl_NL python', the
error does not occur.

http://python.org/sf/813449 seems to be the same bug, BTW.
msg8779 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-06-05 18:04
Logged In: YES 
user_id=1188172

As this is not Windows specific, setting Category to Library.
msg8780 - (view) Author: Adam Monsen (meonkeys) Date: 2005-09-13 22:27
Logged In: YES 
user_id=259388

I'm seeing this error on Fedora Core 4:

Python 2.4.1 (#1, May 16 2005, 15:19:29) 
[GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import locale; locale.getdefaultlocale()
('en_US', 'utf')
>>> locale.resetlocale()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/locale.py", line 389, in resetlocale
    _setlocale(category, _build_localename(getdefaultlocale()))
locale.Error: unsupported locale setting
msg8781 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-02-20 19:09
Logged In: YES 
user_id=849994

The same applies to other locales.

From #813449 (a duplicate of this):
"""
This is a known limitation: getdefaultlocale should not be
used in new code.

If the intention is to compute the locale's encoding,
locale.getpreferredencoding should be used instead.
"""
msg114172 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-08-17 21:22
Still a problems on py3k.  Set stage to needs patch as it's so easy to reproduce.
msg185023 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2013-03-23 00:53
Can we simply document that getpreferredencoding should be used instead as stated in msg8781 ?  Should getdefaultlocale be earmarked for deprecation?
msg185041 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2013-03-23 12:33
Adding support for locales that are not recognized is easy and the locale parser could also learn about formats that it doesn't yet understand, so patches are welcome.

The main problem here is that setlocale() only understands a very limited set of locale names.

Please note that locale.getdefaultlocale() is not the same as locale.getpreferredencoding():

* getdefaultlocale() aims to find the default locale settings for a program which has not yet called setlocale(LC_ALL, "").

* getpreferredencoding() tries to make an educated guess at the encoding the user has setup for his/her environment. It works well on Windows, but on Unix, requires a call to setlocale() to implement the environment variable parsing, which is exactly what getdefaultlocale() tries to avoid.
setlocale() is not thread-safe, so the approach taken by getpreferredencoding() can have unwanted side-effects.
History
Date User Action Args
2014-02-03 15:43:15BreamoreBoysetnosy: - BreamoreBoy
2013-12-10 18:24:49terry.reedysetversions: + Python 3.3, Python 3.4, - Python 3.1, Python 3.2
2013-03-23 12:33:37lemburgsetnosy: + lemburg
messages: + msg185041
2013-03-23 00:53:51BreamoreBoysetmessages: + msg185023
2010-08-17 21:22:44BreamoreBoysetversions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6
nosy: + BreamoreBoy
messages: + msg114172

assignee: mhammond ->
stage: test needed -> needs patch
2009-02-13 02:24:59ajaksu2setdependencies: + locale.getdefaultlocale doesnt handle all locales gracefully
type: behavior
stage: test needed
versions: + Python 2.6, - Python 2.4
2002-01-16 04:56:12syverecreate