Skip to content

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

Closed
tekinertekin wants to merge 1 commit into
python:mainfrom
tekinertekin:fix-calendar-ob-import
Closed

gh-155245: Fix calendar failing to import when strftime rejects %OB#155269
tekinertekin wants to merge 1 commit into
python:mainfrom
tekinertekin:fix-calendar-ob-import

Conversation

@tekinertekin

@tekinertekin tekinertekin commented Aug 6, 2026

Copy link
Copy Markdown

Fixes #155245.

_localized_month stores the format string and only calls strftime from
__getitem__, so _localized_month('%OB') never raises. The first strftime
call happens further down, in the comparison that detects systems which keep
'%OB' as-is — and that comparison sits in the else branch, outside the
try block meant to catch the failure:

try:
    standalone_month_name = _localized_month('%OB')   # no strftime call yet
    ...
except ValueError:                                    # unreachable
    ...
else:
    if len(set(standalone_month_name)) != ...:        # strftime happens HERE

So on a platform whose strftime rejects %OB, the ValueError escapes and
import calendar fails outright. This is not specific to Wine — as written,
the try/except is unreachable on any such platform.

The fix moves the comparison into the try block so the intended fallback to
month_name/month_abbr applies. No behaviour change where %OB works.

Verified locally on a main build (3.16.0a0) by substituting a datetime.date
subclass whose strftime raises ValueError for %O formats, which reproduces
the reported traceback at calendar.py:156 exactly. The added regression test
uses that substitution plus import_helper.import_fresh_module('calendar'), so
it needs no unusual platform: it fails on unpatched main and passes with the
fix. Full test_calendar passes (86 tests) and Tools/patchcheck is clean.

AI tools were used on this PR: Claude Code (Opus) located the cause, wrote the
patch and the test, and ran the verification. I reviewed the change, understand
it and can explain it, and I take responsibility for it.

… %OB

_localized_month stores the format string and calls strftime lazily from
__getitem__, so constructing _localized_month('%OB') never raises. The
first strftime call happens further down, while comparing the standalone
names against the regular ones to detect systems that keep '%OB' as-is --
and that comparison sits in the else branch, outside the try block meant
to catch the failure. On a platform whose strftime rejects '%OB' the
ValueError therefore escaped and importing calendar failed outright.

Move the comparison into the try block so the intended fallback to
month_name/month_abbr applies.
Comment thread Lib/test/test_calendar.py
self.assertListEqual(list(calendar.month_abbr),
list(calendar.standalone_month_abbr))

def test_standalone_month_fallback_when_specifier_rejected(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.

This test isn't really relevant. It would be better to find a locale for which we could check the issue directly without mocking.

@picnixz

picnixz commented Aug 6, 2026

Copy link
Copy Markdown
Member

I consider a PR entirely written by an agent as not acceptable. The contribution should be credited to the agent in this case. We do not want to encourage users to use agents just to add a contribution line in their CV. We rather encourage human-driven contributions than automated ones.

@bedevere-app

bedevere-app Bot commented Aug 6, 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.

@picnixz picnixz closed this Aug 6, 2026
Sign up for free to 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

2 participants