Riccardo Polignieri <ric....@libero.it> added the comment:

> `locale.getlocale()` is now returning strange results

Not really "strange results" - fact is, now "getlocale()" returns the locale 
name *as if* it were already set from the beginnning (because it is, at least 
in part). 

Before: 

>>> import locale  # Python 3.7, new shell
>>> locale.getlocale()
(None, None)
>>> locale.setlocale(locale.LC_ALL, '') # say Hi from Italy
'Italian_Italy.1252'
>>> locale.getlocale()
('Italian_Italy', '1252')

now: 

>>> import locale  # Python 3.8, new shell
>>> locale.getlocale()
('Italian_Italy', '1252')

As for why returned locale names are "a little different" in Windows, I found 
no better explanation that Eryk Sun's essays in 
https://bugs.python.org/issue37945. Long story short, it's not even a bug 
anymore... it's a hot mess and it won't be solved anytime soon. 
But it's not the problem at hand, here. Returned locale names have not changed 
between 3.7 and 3.8. 



What *is* changed, though, is that now Python on Windows appears to set the 
locale, implicitly, right from the start. 
Except - maybe it does not, really: 

>>> import locale  # Python 3.8, new shell
>>> locale.getlocale()
('Italian_Italy', '1252')
>>> locale.localeconv()
{'int_curr_symbol': '', 'currency_symbol': '', 'mon_decimal_point': '', 
'mon_thousands_sep': '', 'mon_grouping': [], 'positive_sign': '', 
'negative_sign': '', 'int_frac_digits': 127, 'frac_digits': 127, 
'p_cs_precedes': 127, 'p_sep_by_space': 127, 'n_cs_precedes': 127, 
'n_sep_by_space': 127, 'p_sign_posn': 127, 'n_sign_posn': 127, 'decimal_point': 
'.', 'thousands_sep': '', 'grouping': []}

As you can see, we have an Italian locale only in the name: the conventions are 
still those of the default C locale. 
If we explicitly set the locale, on the other hand... 

>>> locale.setlocale(locale.LC_ALL, '')
'Italian_Italy.1252'
>>> locale.localeconv()
{'int_curr_symbol': 'EUR', 'currency_symbol': '€', ... ... }

... now we enjoy a real Italian locale - pizza, pasta, gelato and all. 



What happened? 
Unfortunately, this change of behaviour is NOT documented, except for a passing 
note here: https://docs.python.org/3/whatsnew/changelog.html#id144. It's buried 
*very* deep: 
"""
bpo-34485: On Windows, the LC_CTYPE is now set to the user preferred locale at 
startup. Previously, the LC_CTYPE locale was “C” at startup, but changed when 
calling setlocale(LC_CTYPE, “”) or setlocale(LC_ALL, “”).
"""
This explains... something. Python now pre-sets *only* the LC_CTYPE category, 
and that's why the other conventions remain unchanged. 
Unfortunately, setting *that* one category changes the result yielded by 
locale.getlocale(). But this is not a bug either, because it's the same 
behaviour you would have in Python 3.7: 

>>> locale.setlocale(locale.LC_CTYPE, '')  # Python 3.7
'Italian_Italy.1252'
>>> locale.getlocale()
('Italian_Italy', '1252')

...and that's because locale.getlocale() with no arguments default, wait for 
it, to getlocale(category=LC_CTYPE), as documented!



So, why Python 3.8 now pre-sets LC_CTYPE on Windows? Apparently, bpo-34485 is 
part of the ongoing shakespearian feud between Victor Stinner and the Python 
locale code. If you squint hard enough, you will see the answer here: 
https://vstinner.github.io/locale-bugfixes-python3.html but at this point, I 
don't know if anyone still keeps the score. 


To sum up: 
- there's nothing new about locale names - still the same mess; 
- if locale names as returned by locale.getlocale() bother you, you should 
follow Victor's advice here: https://bugs.python.org/issue37945#msg361806. Use 
locale.setlocale(category, None) instead; 
- if you relied on getlocale() with no arguments to test your locale, assuming 
that either a locale is unset or it is "fully set", then you should stop now. A 
locale can also be "partially set", and in fact it's just what happens now on 
Windows by default. You should test for a specific category instead; 
- changing the way the locale is set by default on Windows can be... rather 
surprising and can lead to misunderstandings. I would certainly add a note in 
the locale documentation to explain this new behaviour.

----------
nosy: +ricpol

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38805>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to