Skip to content

Commit bcf985c

Browse files
gh-155245: Add regression test for lazy %OB strftime failure
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.
1 parent b8dd4ab commit bcf985c

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_calendar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,33 @@ def test_standalone_month_name_and_abbr_C_locale(self):
644644
self.assertListEqual(list(calendar.month_abbr),
645645
list(calendar.standalone_month_abbr))
646646

647+
def test_standalone_month_name_survives_lazy_OB_failure(self):
648+
# gh-155245: some platforms accept '%OB'/'%Ob' when a
649+
# _localized_month object is constructed, but the underlying
650+
# strftime() call -- which only happens lazily, inside set() --
651+
# can still raise ValueError once it actually runs (e.g. under
652+
# Wine). Reloading the calendar module used to let that
653+
# ValueError propagate instead of falling back to
654+
# month_name/month_abbr.
655+
import importlib
656+
657+
real_date = datetime.date
658+
659+
class FakeDate(real_date):
660+
def strftime(self, fmt):
661+
if '%O' in fmt:
662+
raise ValueError('Invalid format string')
663+
return super().strftime(fmt)
664+
665+
datetime.date = FakeDate
666+
try:
667+
importlib.reload(calendar)
668+
finally:
669+
datetime.date = real_date
670+
importlib.reload(calendar)
671+
672+
self.assertEqual(calendar.standalone_month_name[1], calendar.month_name[1])
673+
647674
def test_locale_text_calendar(self):
648675
try:
649676
cal = calendar.LocaleTextCalendar(locale='')

0 commit comments

Comments
 (0)