Skip to content

Commit b8dd4ab

Browse files
gh-155245: Fix calendar failing to import when strftime rejects %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.
1 parent af930c1 commit b8dd4ab

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

Lib/calendar.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,20 @@ def __len__(self):
150150
standalone_month_name = month_name
151151
standalone_month_abbr = month_abbr
152152
else:
153-
# Some systems that do not support '%OB' will keep it as-is (i.e.,
154-
# we get [..., '%OB', '%OB', '%OB']), so for non-distinct names,
155-
# we fall back to month_name/month_abbr.
156-
if len(set(standalone_month_name)) != len(set(month_name)):
157-
standalone_month_name = month_name
158-
if len(set(standalone_month_abbr)) != len(set(month_abbr)):
159-
standalone_month_abbr = month_abbr
153+
# Some systems that do not support '%OB' will keep it as-is (i.e.,
154+
# we get [..., '%OB', '%OB', '%OB']), so for non-distinct names,
155+
# we fall back to month_name/month_abbr. The strftime() calls
156+
# inside set() are evaluated lazily here, so on platforms that
157+
# accept '%OB' at construction time but reject it later (e.g.
158+
# under Wine), this can still raise ValueError.
159+
try:
160+
if len(set(standalone_month_name)) != len(set(month_name)):
161+
standalone_month_name = month_name
162+
if len(set(standalone_month_abbr)) != len(set(month_abbr)):
163+
standalone_month_abbr = month_abbr
164+
except ValueError:
165+
standalone_month_name = month_name
166+
standalone_month_abbr = month_abbr
160167

161168

162169
def isleap(year):

0 commit comments

Comments
 (0)