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 — gh-155245: Fix calendar failing to import when strftime rejects %OB by Sudeep-A-Kulkarni · Pull Request #155317 · python/cpython

Conversation

@Sudeep-A-Kulkarni

@Sudeep-A-Kulkarni Sudeep-A-Kulkarni commented Aug 7, 2026

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.

Fixes gh-155245

Sorry, something went wrong.

Uh oh!

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

All reactions
Sudeep-A-Kulkarni added 3 commits August 7, 2026 11:33
@Sudeep-A-Kulkarni
pythongh-155245: Fix calendar failing to import when strftime rejects…
b8dd4ab
… %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.
@Sudeep-A-Kulkarni
pythongh-155245: Add regression test for lazy %OB strftime failure
bcf985c
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.
@Sudeep-A-Kulkarni
pythongh-155245: Add NEWS entry for calendar %OB fix
db89848
@Sudeep-A-Kulkarni
Sudeep-A-Kulkarni requested a review from AA-Turner as a code owner August 7, 2026 06:57
@python-cla-bot

python-cla-bot Bot commented Aug 7, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot mentioned this pull request Aug 7, 2026
calendar module fails to import on Wine due to incomplete %OB error handling #155245
Open
@bedevere-app bedevere-app Bot added the awaiting review label Aug 7, 2026
Sudeep-A-Kulkarni added 3 commits August 7, 2026 12:28
@Sudeep-A-Kulkarni
Merge branch 'main' into pythongh-155245-fix-calendar-OB
2cbfa63
@Sudeep-A-Kulkarni
pythongh-155245: Remove trailing whitespace in test_calendar.py
3c15175
@Sudeep-A-Kulkarni
Merge branch 'main' into pythongh-155245-fix-calendar-OB
c1b6fe4
@Sudeep-A-Kulkarni

Sudeep-A-Kulkarni commented Aug 8, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

picnixz
picnixz reviewed Aug 8, 2026
View reviewed changes
Comment thread Lib/test/test_calendar.py Resolved

Uh oh!

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

picnixz
picnixz requested changes Aug 8, 2026
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

  • 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.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting changes and removed awaiting review labels Aug 8, 2026
@bedevere-app

bedevere-app Bot commented Aug 8, 2026

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!

All reactions

Sorry, something went wrong.

Uh oh!

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

@Sudeep-A-Kulkarni

Sudeep-A-Kulkarni commented Aug 10, 2026

Copy link
Copy Markdown
Author

Hi picnixz, sorry for the delay in answering this directly.

I looked into whether a real locale could exercise this instead of mocking, and I don't think one exists for this case. The root cause here isn't locale-specific — it's that Wine's strftime() doesn't support the %O modifier at all, on any locale. glibc, macOS's libc, and Windows's CRT all support %O, so on any environment CI actually runs on, %OB just works and never raises. The only way to get the real ValueError is to run under Wine itself, which isn't something we can do in standard CI.

That's why I went with subclassing datetime.date to make strftime raise on %O, rather than picking a locale — it's simulating Wine's missing feature, not a locale quirk. If there's a way to spin up a Wine-based CI job for this I'm open to it, but I don't think a locale swap alone gets us there.

I'll also fix the indentation simitana flagged.

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting change review and removed awaiting changes labels Aug 10, 2026
@bedevere-app

bedevere-app Bot commented Aug 10, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app
bedevere-app Bot requested a review from picnixz August 10, 2026 05:56
Sudeep-A-Kulkarni added 2 commits August 17, 2026 11:49
@Sudeep-A-Kulkarni
Merge branch 'main' into pythongh-155245-fix-calendar-OB
1653f52
@Sudeep-A-Kulkarni
Merge branch 'main' into pythongh-155245-fix-calendar-OB
9660b8a
@picnixz

picnixz commented Aug 19, 2026

Copy link
Copy Markdown
Member

Please don't hit the button "Update branch", it makes useless notifications: https://devguide.python.org/getting-started/pull-request-lifecycle/#update-branch-button. As for the PR: don't use importlib, use mocks and import_fresh_module helpers.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Sudeep-A-Kulkarni
pythongh-155245: Use mock.patch and import_fresh_module instead of im…
84be097
…portlib.reload

Address review feedback from picnixz: rather than monkeypatching datetime.date directly and reloading the shared calendar module with importlib.reload() (which mutated the module for the rest of the test process), patch the 'datetime.date' name with mock.patch() and use test.support.import_helper.import_fresh_module() to get an isolated copy of calendar for the assertion.
encukou
encukou reviewed Sep 14, 2026
View reviewed changes

@encukou encukou left a comment

Copy link
Copy Markdown
Member

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.

Thanks.
This test doesn't look very useful: it relies on the internals, so even with the test, changes to calendar's logic can still break things on Wine.

Per PEP 11 we can accept their patches that make CPython work on Wine, but testing/supporting/backporting is on whomever builds/runs there.

I'd delete the test from here.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/calendar.py
Comment on lines +160 to +163
if len(set(standalone_month_name)) != len(set(month_name)):
standalone_month_name = month_name
if len(set(standalone_month_abbr)) != len(set(month_abbr)):
standalone_month_abbr = month_abbr

@encukou encukou Sep 14, 2026

Copy link
Copy Markdown
Member

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.

AFAICS, you can put these lines in the try block, instead of adding a new block.

Sorry, something went wrong.

Uh oh!

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

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

Reviewers

@encukou encukou encukou left review comments
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@picnixz picnixz Awaiting requested review from picnixz

Uh oh!

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

Assignees

No one assigned

Labels

awaiting change review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

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

3 participants

@Sudeep-A-Kulkarni @picnixz @encukou
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.