gh-155245: Fix calendar failing to import when strftime rejects %OB - #155317
gh-155245: Fix calendar failing to import when strftime rejects %OB#155317Sudeep-A-Kulkarni wants to merge 6 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.
|
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. |
|
Logic looks right, but checked the indentation on the branch: the body of |
| self.assertListEqual(list(calendar.month_abbr), | ||
| list(calendar.standalone_month_abbr)) | ||
|
|
||
| def test_standalone_month_name_survives_lazy_OB_failure(self): |
There was a problem hiding this comment.
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.
picnixz
left a comment
There was a problem hiding this comment.
- 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.
|
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 And if you don't make the requested changes, you will be poked with soft cushions! |
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