Skip to content

gh-155245: Fix calendar failing to import when strftime rejects %OB - #155317

Open
Sudeep-A-Kulkarni wants to merge 6 commits into
python:mainfrom
Sudeep-A-Kulkarni:gh-155245-fix-calendar-OB
Open

gh-155245: Fix calendar failing to import when strftime rejects %OB#155317
Sudeep-A-Kulkarni wants to merge 6 commits into
python:mainfrom
Sudeep-A-Kulkarni:gh-155245-fix-calendar-OB

Conversation

@Sudeep-A-Kulkarni

Copy link
Copy Markdown

Summary

calendar.standalone_month_name/standalone_month_abbr are built by constructing a _localized_month-like object with the %OB/%Ob format codes. On some platforms (e.g. under Wine), strftime() accepts %OB at construction time but raises ValueError later, when the value is actually formatted. That later formatting only happens lazily, inside the set() calls in the else branch used to decide whether to fall back to month_name/month_abbr. Because that set() logic wasn't wrapped in a try/except, the ValueError propagated uncaught, causing import calendar itself to fail (gh-155245).

Fix

Wrap the set() comparison logic in a nested try/except ValueError, falling back to month_name/month_abbr if formatting fails at that point, same as the existing fallback for construction-time failures.

Testing

Added test_standalone_month_name_survives_lazy_OB_failure in Lib/test/test_calendar.py, which patches datetime.date.strftime (via a subclass, since datetime.date is immutable) to raise ValueError for any %O format code, reloads the calendar module, and asserts that standalone_month_name falls back to month_name instead of raising.

I reproduced the original failure locally first (confirmed the uncaught ValueError before the fix, and confirmed the fix resolves it) before writing this PR, per the guidance in the issue thread.

Fixesgh-155245

… %OB
The strftime() call for standalone month names ('%OB'/'%Ob') is evaluated lazily inside set(), outside the try/except that guards the initial _localized_month construction. On platforms where '%O' is accepted at construction but rejected when actually formatting (e.g. under Wine), this raised an uncaught ValueError. Wrap the set() comparisons in their own try/except so we fall back to month_name/month_abbr in that case too.
Adds test_standalone_month_name_survives_lazy_OB_failure to OutputTestCase. It simulates a platform (like Wine) where strftime() accepts '%OB' when a _localized_month is constructed but raises ValueError once the lazy strftime call actually happens inside set(), by reloading the calendar module with a patched datetime.date. Without the fix in calendar.py this test fails with an uncaught ValueError.
@python-cla-bot

python-cla-botBot commented Aug 7, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@Sudeep-A-Kulkarni

Copy link
Copy Markdown
Author

Hello, I wanted to follow up on this pull request, which has been awaiting review for some time. Please let me know if any changes are needed, or if there is additional information I can provide to help move it forward. Thank you for your time and consideration.

@simitana

Copy link
Copy Markdown

Logic looks right, but checked the indentation on the branch: the body of else: is indented 8 spaces instead of 4, inconsistent with the rest of the file and with the except ValueError: block right above it at the same nesting level. Still parses fine since it's just a deeper single block, but it reads like it's nested under something it isn't. Please re-indent to 4 spaces.

self.assertListEqual(list(calendar.month_abbr),
list(calendar.standalone_month_abbr))

def test_standalone_month_name_survives_lazy_OB_failure(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there really no way to test this with real locales instead of fake ones like that? it's exactly the same as the PR i closed. As I said, don't let your agents run in automode and I believe this has not been followed. So confirm first whether we can use a true locale for this test.

@picnixzpicnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • Tell me (you as a human, without any LLM) whether we can have a test that doesn't rely on mocking.
  • You don't need to change the comments.

@bedevere-app

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calendar module fails to import on Wine due to incomplete %OB error handling

3 participants

@Sudeep-A-Kulkarni@simitana@picnixz