GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-46659: Enhance LocaleTextCalendar for C locale by vstinner · Pull Request #31214 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Feb 8, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

https://bugs.python.org/issue46659

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Feb 8, 2022
@bedevere-bot bedevere-bot added the awaiting core review label Feb 8, 2022
@vstinner vstinner added skip news and removed CLA signed awaiting core review labels Feb 8, 2022
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Feb 8, 2022
@vstinner

vstinner commented Feb 8, 2022

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka @eryksun

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eryksun
eryksun reviewed Feb 8, 2022
View reviewed changes
Comment thread Lib/calendar.py Outdated
Comment on lines 554 to 565

@eryksun eryksun Feb 8, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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 to capture the preferred locale when it's created.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Feb 9, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eryksun eryksun Feb 9, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The previous code based on getdefaultlocale() was a one time check of LC_ALL, LC_CTYPE, and LANG. This was technically wrong, but in practice the default LC_TIME is usually the same as the default LC_CTYPE.

I think it's correct to use the current LC_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 instantiating LocaleTextCalendar() when LC_TIME is the initial "C" value, which is usually the case if a script doesn't explicitly set LC_ALL or LC_TIME.

But changing it to re-evaluate the default via setlocale(LC_TIME, "") for every formatweekday() call is going too far. That depends on the current values of the environment variables LC_ALL, LC_TIME, and LANG, which is very different from the one-time getdefaultlocale() call in the previous code.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eryksun
eryksun reviewed Feb 8, 2022
View reviewed changes
Comment thread Lib/calendar.py Outdated
Comment on lines 568 to 583

@eryksun eryksun Feb 8, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggestion:

if locale is None:
    locale = _locale.getlocale(_locale.LC_TIME)
    if locale == (None, None):
        with different_locale(""):
            locale = _locale.getlocale(_locale.LC_TIME)
self.locale = locale

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Feb 9, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eryksun eryksun Feb 9, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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 modify LC_TIME in __init__(), but fine to do so in formatweekday() and formatmonthname()? The class is documented as not thread safe with respect to the process locale.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Feb 9, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eryksun eryksun Feb 9, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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 via getdefaultlocale() only once, instead of calling it on every entry of different_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 the entire default behavior of locale=None should change to use the LC_TIME locale even if it's "C".

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
bpo-46659: Enhance LocaleTextCalendar for C locale
cfd46fc
If the LC_TIME locale is "C", use the user preferred locale.
@vstinner

vstinner commented Feb 22, 2022

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Feb 22, 2022

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit 4fccf91 into python:main Feb 24, 2022
@vstinner
vstinner deleted the calendar_c_locale branch February 24, 2022 13:29
@vstinner

vstinner commented Feb 24, 2022

Copy link
Copy Markdown
Member Author

I merged my PR. Thanks @eryksun for your feedback and the nice idea of reusing with 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

asvetlov pushed a commit that referenced this pull request Feb 26, 2022
@vstinner @asvetlov
bpo-46659: Enhance LocaleTextCalendar for C locale (GH-31214)
a1a69b5
If the LC_TIME locale is "C", use the user preferred locale.
@AA-Turner AA-Turner mentioned this pull request Jun 9, 2022
gh-69714: Make calendar module fully tested #93655
Merged
@vstinner vstinner mentioned this pull request Dec 11, 2022
Deprecate locale.getdefaultlocale() function #90817
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eryksun eryksun eryksun left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@vstinner @eryksun @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.