GeezerVison is showing github.com — locale.getlocale fails if locale is set · Issue #87281 · python/cpython
locale.getlocale fails if locale is set #87281Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2021-02-03.13:42:50.296>
labels = ['3.8', '3.10', 'library', '3.9', 'OS-windows']
title = 'locale.getlocale fails if locale is set'
updated_at = <Date 2021-02-23.17:59:57.570>
user = 'https://github.com/AndersMunch'
bugs.python.org fields:
activity = <Date 2021-02-23.17:59:57.570>
actor = 'eryksun'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)', 'Windows']
creation = <Date 2021-02-03.13:42:50.296>
creator = 'AndersMunch'
dependencies = []
files = []
hgrepos = []
issue_num = 43115
keywords = []
message_count = 15.0
messages = ['386203', '387136', '387138', '387147', '387149', '387157', '387164', '387195', '387205', '387208', '387209', '387256', '387273', '387558', '387588']
nosy_count = 8.0
nosy_names = ['lemburg', 'paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'swt2c', 'AndersMunch']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue43115'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']
Linked PRs
ActivityAndersMunch commented on Feb 3, 2021 MannequinAuthor More actions getlocale fails with an exception when the string returned by _setlocale is already an RFC 1766 language tag.
Example:
Python 3.10.0a4 (tags/v3.10.0a4:445f7f5, Jan 4 2021, 19:55:53) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en-US')
'en-US'
>>> locale.getlocale()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\flonidan\env\Python310\lib\locale.py", line 593, in getlocale
return _parse_localename(localename)
File "C:\flonidan\env\Python310\lib\locale.py", line 501, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: en-US
Expected result:
('en-US', None)
See wxWidgets/Phoenix#1637 for an example of the ensuing problems. wx.Locale calls C setlocale directly, but, as far as I can see, correctly, using dashes in the language code which is consistent with not only RFC 1766 but also the examples in Microsoft's documentation (https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-160). CPython seems to assume underscored names such as 'en_US' instead, as shown by getdefaultlocale inserting an underscore. AndersMunch commented on Feb 17, 2021 MannequinAuthor More actions I discovered that this can happen with underscores as well:
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_DE')
'en_DE'
>>> locale.getlocale()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\flonidan\env\Python38-64\lib\locale.py", line 591, in getlocale
return _parse_localename(localename)
File "C:\flonidan\env\Python38-64\lib\locale.py", line 499, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: en_DE
locale.setlocale does validate input - if you write nonsense in the second argument then you get an exception, "locale.Error: unsupported locale setting". So I'm guessing the en_DE locale is actually being set here, and the problem is solely about getlocale making unfounded assumptions about the format.
Same thing happens in 3.10.0a4. malemburg commented on Feb 17, 2021 On 17.02.2021 10:55, Anders Munch wrote:
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, 'en_DE')
> 'en_DE'
>>>> locale.getlocale()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "C:\flonidan\env\Python38-64\lib\locale.py", line 591, in getlocale
> return _parse_localename(localename)
> File "C:\flonidan\env\Python38-64\lib\locale.py", line 499, in _parse_localename
> raise ValueError('unknown locale: %s' % localename)
> ValueError: unknown locale: en_DE
The locale module does not know this encoding, so cannot
guess the encoding. Since getlocale() returns the language
code and encoding, this fails.
If you add the encoding, you should be fine:
>>> locale.setlocale(locale.LC_ALL, 'en_DE.UTF-8')
'en_DE.UTF-8'
>>> locale.getlocale()
('en_DE', 'UTF-8')AndersMunch commented on Feb 17, 2021 MannequinAuthor More actions getlocale is documented to return None for the encoding if no encoding can be determined. There's no need to guess.
I can't change the locale.setlocale call, because where I'm actually having the problem, I'm not even calling locale.setlocale: wxWidgets is calling C setlocale, that's where it comes from.
wxWidgets aside, it doesn't seem right that getlocale fails when setlocale succeeded. malemburg commented on Feb 17, 2021 added a commit that references this issue on Aug 13, 2025gh-87281: Improve documentation for locale.setlocale() and locale.get… StanFromIreland commented on Sep 17, 2025 Triage: Is there anything left to do here? All backports are complete. serhiy-storchaka commented on Sep 17, 2025 Yes, this is a large and complex issue. It needs a lot of work.
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|