Repository navigation
gh-64376: Use Argument Clinic for datetime.date classmethods #20208
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
Changes from 1 commit
ee232ca
78e71d0
d0eac8d
5d9b7de
0497383
13ad7c7
c1a2b1e
e6f7572
11f83fd
188130b
0528066
d6c8759
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Note that this will now throw OverflowError instead of ValueError when using the C version of datetime. There is precedent for this, e.g., datetime.date.fromordinal will throw an OverflowError with the C module but ValueError with the Python.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3022,25 +3022,24 @@ datetime_date_fromisoformat(PyTypeObject *type, PyObject *date_string) | |
| } | ||
|
|
||
|
|
||
| static PyObject * | ||
| date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) | ||
| { | ||
| static char *keywords[] = { | ||
| "year", "week", "day", NULL | ||
| }; | ||
| /*[clinic input] | ||
| @classmethod | ||
| datetime.date.fromisocalendar | ||
|
|
||
| int year, week, day; | ||
| if (PyArg_ParseTupleAndKeywords(args, kw, "iii:fromisocalendar", | ||
| keywords, | ||
| &year, &week, &day) == 0) { | ||
| if (PyErr_ExceptionMatches(PyExc_OverflowError)) { | ||
| PyErr_Format(PyExc_ValueError, | ||
| "ISO calendar component out of range"); | ||
| year: int | ||
| week: int | ||
| day: int | ||
|
|
||
| } | ||
|
Comment on lines
-3020
to
-3024
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I see that before this PR we have code in I think we should find a way to retain this behavior, even if that means not using Argument Clinic for this method right now. |
||
| return NULL; | ||
| } | ||
| int, int, int -> Construct a date from the ISO year, week number and weekday. | ||
|
|
||
| This is the inverse of the date.isocalendar() function | ||
|
hauntsaninja marked this conversation as resolved.
Outdated
|
||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject * | ||
| datetime_date_fromisocalendar_impl(PyTypeObject *type, int year, int week, | ||
| int day) | ||
| /*[clinic end generated code: output=7b26e15115d24df6 input=f13669bbed6c8bb6]*/ | ||
| { | ||
| // Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5) | ||
| if (year < MINYEAR || year > MAXYEAR) { | ||
| PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year); | ||
|
|
@@ -3078,7 +3077,7 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) | |
|
|
||
| ord_to_ymd(day_1 + day_offset, &year, &month, &day); | ||
|
|
||
| return new_date_subclass_ex(year, month, day, cls); | ||
| return new_date_subclass_ex(year, month, day, (PyObject *) type); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -3503,12 +3502,7 @@ static PyMethodDef date_methods[] = { | |
| DATETIME_DATE_FROMTIMESTAMP_METHODDEF | ||
| DATETIME_DATE_FROMORDINAL_METHODDEF | ||
| DATETIME_DATE_FROMISOFORMAT_METHODDEF | ||
|
|
||
| {"fromisocalendar", (PyCFunction)(void(*)(void))date_fromisocalendar, | ||
| METH_VARARGS | METH_KEYWORDS | METH_CLASS, | ||
| PyDoc_STR("int, int, int -> Construct a date from the ISO year, week " | ||
| "number and weekday.\n\n" | ||
| "This is the inverse of the date.isocalendar() function")}, | ||
| DATETIME_DATE_FROMISOCALENDAR_METHODDEF | ||
|
|
||
| {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, | ||
| PyDoc_STR("Current date or datetime: same as " | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mentioned this in a PR comment:
Re:
datetime.date.fromisocalendarNote that this now can throw OverflowError instead of ValueError when
using the C version of datetime. There is precedent for this, e.g.,
datetime.date.fromordinalwill throw an OverflowError with the C modulebut ValueError with the Python.
Also, prior to this PR, the C version
fromisocalendarwould throw a ValueError while the Python version throws TypeError for issues with e.g, missing arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks, I didn't make the connection! I'll take another look at that.