gh-155245: Fix calendar failing to import when strftime rejects %OB - #155317
Open
Sudeep-A-Kulkarni wants to merge 5 commits into
Open
gh-155245: Fix calendar failing to import when strftime rejects %OB#155317Sudeep-A-Kulkarni wants to merge 5 commits into
Sudeep-A-Kulkarni wants to merge 5 commits into
Conversation
… %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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
calendar.standalone_month_name/standalone_month_abbrare built by constructing a_localized_month-like object with the%OB/%Obformat codes. On some platforms (e.g. under Wine),strftime()accepts%OBat construction time but raisesValueErrorlater, when the value is actually formatted. That later formatting only happens lazily, inside theset()calls in theelsebranch used to decide whether to fall back tomonth_name/month_abbr. Because thatset()logic wasn't wrapped in atry/except, theValueErrorpropagated uncaught, causingimport calendaritself to fail (gh-155245).Fix
Wrap the
set()comparison logic in a nestedtry/except ValueError, falling back tomonth_name/month_abbrif formatting fails at that point, same as the existing fallback for construction-time failures.Testing
Added
test_standalone_month_name_survives_lazy_OB_failureinLib/test/test_calendar.py, which patchesdatetime.date.strftime(via a subclass, sincedatetime.dateis immutable) to raiseValueErrorfor any%Oformat code, reloads thecalendarmodule, and asserts thatstandalone_month_namefalls back tomonth_nameinstead of raising.I reproduced the original failure locally first (confirmed the uncaught
ValueErrorbefore the fix, and confirmed the fix resolves it) before writing this PR, per the guidance in the issue thread.Fixes gh-155245