-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate locale.getdefaultlocale() function #90817
Comments
The locale.getdefaultlocale() function only relies on environment variables. At Python startup, Python calls setlocale() is set the LC_CTYPE locale to the user preferred encoding. Since Python 3.7, if the LC_CTYPE locale is "C" or "POSIX", PEP-538 sets the LC_CTYPE locale to a UTF-8 variant if available, and PEP-540 ignores the locale and forces the usage of the UTF-8 encoding. The *effective* encoding used by Python is inconsistent with environment variables. Moreover, if setlocale() is called to set the LC_CTYPE locale to a locale different than the user locale, again, environment variables are inconsistent with the effective locale. In these cases, locale.getdefaultlocale() result is not the expected locale and it can lead to mojibake and other issues. For these reasons, I propose to deprecate locale.getdefaultlocale(): setlocale(), getpreferredencoding() and getlocale() should be used instead. For the background on these issues, see recent issue: |
cal_locale.py: Test calendar.LocaleTextCalendar() default locale, manual test for #75349. |
Please see the discussion on https://bugs.python.org/issue43552: locale.getpreferredencoding() needs to be deprecated as well. Instead we should have a single locale.getencoding() as outlined there... perhaps in a separate ticket ?! Thanks. |
Yeah, I read this issue. But these things are too complicated :-) I prefer to move step by step. Once locale.getencoding() (or a similar function) is added, we can update the deprecation message. I hope to be able to deprecate getdefaultlocale() and to add such new function in Python 3.11. |
getdefaultlocale() falls back to LANG and LANGUAGE. It allows also to specify a list of looked up environment variables. How could this use case be covered with getlocale()? |
_Py_SetLocaleFromEnv(LC_CTYPE) (e.g. setlocale(LC_CTYPE, "")) gets called at startup, except for the isolated configuration [1]. I think calendar.Locale*Calendar should try the LC_CTYPE locale if LC_TIME is "C", i.e. (None, None). Otherwise, it's introducing new default behavior. For example, with LC_ALL set to "ru_RU.utf8": 3.8: >>> locale.getlocale(locale.LC_TIME)
(None, None)
>>> locale.getlocale(locale.LC_CTYPE)
('ru_RU', 'UTF-8')
>>> cal = calendar.LocaleTextCalendar()
>>> cal.formatweekday(0, 15)
' Понедельник ' 3.11.0a5+: >>> locale.getlocale(locale.LC_TIME)
(None, None)
>>> locale.getlocale(locale.LC_CTYPE)
('ru_RU', 'UTF-8')
>>> cal = calendar.LocaleTextCalendar()
>>> cal.formatweekday(0, 15)
' Monday '
>>> locale.setlocale(locale.LC_TIME, '')
'ru_RU.utf8'
>>> cal = calendar.LocaleTextCalendar()
>>> cal.formatweekday(0, 15)
' Понедельник ' [1] https://docs.python.org/3/c-api/init_config.html?#isolated-configuration |
Serhiy: "getdefaultlocale() falls back to LANG and LANGUAGE. It allows also to specify a list of looked up environment variables. How could this use case be covered with getlocale()?" What's your use case to use env vars rather than the current LC_CTYPE locale? My concern is that when setlocale() is called, the current LC_CTYPE locale is inconsistent and you can get mojibake and others issues. See for example: Marc-Andre Lemburg wants to deprecate it: |
Oh. Serhiy asked me to use LC_TIME rather than LC_CTYPE. See also my example in the PR: |
Since Locale*Calendar is documented as not being thread safe, __init__() could get the real default via setlocale(LC_TIME, "") when locale=None and the current LC_TIME is "C". Restore it back to "C" after getting the default. That should usually match the behavior from previous versions that called getdefaultlocale(). In cases where it differs, it's fixing a bug because the default LC_TIME is the correct default. |
Eryk: I created #75397 which uses the user preferred locale if the current LC_TIME locale is "C" or "POSIX". Moreover, it no longer gets the current locale when the class is created. If locale=locale is passed, just use the current LC_TIME (or the user preferred is the locale is "C" or "POSIX"). |
locale.getdefaultlocale() is now deprecated. calendar now uses locale.setlocale() instead of locale.getdefaultlocale(). The ANSI code page alias to MBCS now has better tests and better comments. Thanks Eryk Sun for your very useful feedback! |
Thanks, Victor. |
The function was already deprecated in Python 3.11 since it calls locale.getdefaultlocale() which was deprecated in Python 3.11.
pythonGH-94960) (cherry picked from commit dc2757a) Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Our project relies on May I request NOT REMOVING |
Return of locale.getlocale() is not compatibile with typical locale string (Python 3.11.7 on Windows):
'it_IT' permits to fastly use gettext ./locale/<it_IT>/... translations mo files. |
That's why the deprecation suggests using locale.setlocale(). Hum, the API is surprising, but locale.setlocale() should be used to get the current locale: |
Maybe the deprecation message should be clarified. I dislike locale.getlocale(), it can return invalid locale which is not accepted by setlocale(). I wanted to deprecate it. |
The point of having getlocale() is to be able to normalize the returned values to make them compatible to setlocale(). The C lib API of setlocale() to actually fetch the current locale values is completely counterintuitive, which is why I had added getlocale() instead. On top of this, setlocale() does not always return values which you can feed back into it. @shinewook: Could you please check what locale._setlocale(locale.LC_TYPE) returns on your Windows system ? It's possible that we'll have to add more aliases to the table to fix this. Thanks. BTW: Perhaps better to open a separate ticket for that problem... |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: