Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-46659: Enhance LocaleTextCalendar for C locale#31214

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 1 commit intopython:mainfromvstinner:calendar_c_locale
Feb 24, 2022
Merged

bpo-46659: Enhance LocaleTextCalendar for C locale#31214

vstinner merged 1 commit intopython:mainfromvstinner:calendar_c_locale
Feb 24, 2022

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedFeb 8, 2022
edited
Loading

If the LC_TIME locale is "C", use the user preferred locale.

https://bugs.python.org/issue46659

@vstinner
Copy link
MemberAuthor

Comment on lines 554 to 565
self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
if self.locale is not None:
_locale.setlocale(_locale.LC_TIME, self.locale)
else:
if self.oldlocale in ("C", "POSIX"):
# The locale does not seem to be configured:
# get the user preferred locale.
_locale.setlocale(_locale.LC_TIME, "")
else:
# Otherwise simply use the current LC_TIME locale.
# The locale is not changed, __exit__() does nothing.
self.oldlocale = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The problem with this is that it can change based on environment variables:

>>>locale.setlocale(locale.LC_TIME,'')'ru_RU.utf8'>>>os.environ['LC_ALL']='en_US.utf8'>>>locale.setlocale(locale.LC_TIME,'')'en_US.utf8'

But the instance is supposed tocapture the preferred locale when it's created.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why is it so important for you to get the locale when Python is started? As you shown, there are many ways to change locales. And using an locale set before all the changes is irrelevant, so it's now outdated, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The previous code based ongetdefaultlocale() was a one time check ofLC_ALL,LC_CTYPE, andLANG. This was technically wrong, but in practice the defaultLC_TIME is usually the same as the defaultLC_CTYPE.

I think it's correct to use the currentLC_TIME locale, if it was configured already. Not doing so was a bug. However, it should try to remain close to the previous default behavior when instantiatingLocaleTextCalendar() whenLC_TIME is the initial "C" value, which is usually the case if a script doesn't explicitly setLC_ALL orLC_TIME.

But changing it to re-evaluate the default viasetlocale(LC_TIME, "") for everyformatweekday() call is going too far. That depends on the current values of the environment variablesLC_ALL,LC_TIME, andLANG, which is very different from the one-timegetdefaultlocale() call in the previous code.

Comment on lines 568 to 583
if locale is None:
locale = _locale.getlocale(_locale.LC_TIME)
self.locale = locale
Copy link
Contributor

@eryksuneryksunFeb 8, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggestion:

iflocaleisNone:locale=_locale.getlocale(_locale.LC_TIME)iflocale== (None,None):withdifferent_locale(""):locale=_locale.getlocale(_locale.LC_TIME)self.locale=locale

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would prefer to not change the locale in the constructor. That's why I proposed this specific PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why is not okay to temporarily modifyLC_TIME in__init__(), but fine to do so informatweekday() andformatmonthname()? The class is documented as not thread safe with respect to the process locale.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It is ok to call setlocale() in__init__(). But setlocale() affects all threads, so I prefer to avoid calling it if possible.

Copy link
Contributor

@eryksuneryksunFeb 9, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think that change is the less of the two. I don't think it's worth changing the behavior from the previous code that looked up the default locale to use viagetdefaultlocale() only once, instead of calling it on every entry ofdifferent_locale. But that's just me voicing one opinion. I'd prefer for other people to way in on exactly which default behaviors are important to preserve, or whether theentire default behavior oflocale=None should change to use theLC_TIME locale even if it's "C".

If the LC_TIME locale is "C", use the user preferred locale.
@vstinner
Copy link
MemberAuthor

@eryksun: I modified the implementation to only get the user preferred locale once, in the constructor. Is it better?

@vstinner
Copy link
MemberAuthor

I tested manually the change on FreeBSD: it works, LC_TIME is also"C" at Python startup.

FreeBSD has a "POSIX" locale, but it's not used when LC_ALL, LC_CTYPE and LANG environment variable at not set: LC_CTYPE is "C" in this case.

@vstinnervstinner merged commit4fccf91 intopython:mainFeb 24, 2022
@vstinnervstinner deleted the calendar_c_locale branchFebruary 24, 2022 13:29
@vstinner
Copy link
MemberAuthor

I merged my PR. Thanks@eryksun for your feedback and the nice idea of reusingwith different_locale(""): to get the user preferred locale :-)

calendar should now be a little bit better in some corner cases, but for the regular use case, there should be no change between Python 3.10 and 3.11.

asvetlov pushed a commit that referenced this pull requestFeb 26, 2022
If the LC_TIME locale is "C", use the user preferred locale.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@eryksuneryksuneryksun left review comments

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@vstinner@eryksun@the-knights-who-say-ni@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp