Skip to content

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

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

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

1 participant