mr_bulrathi
mr_bulrathi

Reputation: 554

monthdays2calendar from calendar module renders OverflowError if year == 1

I have following function

import calendar
def some_function(year):
    y = calendar.Calendar(year)
    print list(y.monthdays2calendar(year, 1))

When I call some_function(1), python says bla-bla-bla OverflowError: date value out of range

Thanks in advance for clue.

Upvotes: 0

Views: 97

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122222

You are passing year to calendar.Calendar(); the class interprets that argument as firstweekday and only accepts 0 (Monday) to 6 (Sunday) as the values.

Pass in 0 and the code works just fine:

>>> import calendar
>>> y = calendar.Calendar(0)
>>> list(y.monthdays2calendar(1, 1))
[[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6)], [(8, 0), (9, 1), (10, 2), (11, 3), (12, 4), (13, 5), (14, 6)], [(15, 0), (16, 1), (17, 2), (18, 3), (19, 4), (20, 5), (21, 6)], [(22, 0), (23, 1), (24, 2), (25, 3), (26, 4), (27, 5), (28, 6)], [(29, 0), (30, 1), (31, 2), (0, 3), (0, 4), (0, 5), (0, 6)]]

That said, there is a problem here with values over 0, as for year == 1 there appears to be a boundary problem in the module. A similar bug for the year 9999 was fixed before; I've filed a new bug for this one.

Upvotes: 2

Related Questions