8000 bpo-36004: Add date.fromisocalendar by pganssle · Pull Request #11888 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-36004: Add date.fromisocalendar #11888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add early year check to fromisocalendar
This avoids an overflow error in ordinal calculations in the C
implementation.
  • Loading branch information
pganssle committed Apr 29, 2019
commit fbab728dfb75723f8ee57f146dbdc31613270470
4 changes: 4 additions & 0 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,10 @@ def fromisoformat(cls, date_string):

@classmethod
def fromisocalendar(cls, year, week, day):
# Year is bounded this way because 9999-12-31 is (9999, 52, 5)
if not 0 < year < 10000:
raise ValueError(f"Year is out of range: {year}")

if not 0 < week < 54:
raise ValueError(f"Invalid week: {week}")

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,9 @@ class KnownFailure(tuple):
(2019, 1, -1),
(2019, 1, 8),
KnownFailure([2019, 53, 1]),
(10000, 1, 1),
(0, 1, 1),
(9999999, 1, 1),
]

for isocal in isocals:
Expand All @@ -1842,9 +1845,6 @@ class KnownFailure(tuple):
raise Exception("XPASS: Known failure condition not met")





#############################################################################
# datetime tests

Expand Down
7 changes: 7 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3028,6 +3028,12 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) {
return NULL;
}

// Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5)
if (year <= 0 || year >= 10000) {
PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year);
return NULL;
}

if (week <= 0 || week >= 54) {
PyErr_Format(PyExc_ValueError, "Invalid week: %d", week);
return NULL;
Expand All @@ -3038,6 +3044,7 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) {
return NULL;
}


int month = week;
isocalendar_to_ymd(&year, &month, &day);

Expand Down
0