From e47ff229b4cba6bee32858448a06e30e3371e34c Mon Sep 17 00:00:00 2001 From: Tan Long Date: Tue, 24 Nov 2026 00:59:18 +0800 Subject: [PATCH 1/2] timedelta/timezone AC vectorcall --- Modules/_datetimemodule.c | 8 +- Modules/clinic/_datetimemodule.c.h | 131 +++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 11 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 091da059afd34b4..ce8babb01821d93 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2833,6 +2833,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, } /*[clinic input] +@vectorcall @classmethod datetime.timedelta.__new__ as delta_new @@ -2854,7 +2855,7 @@ static PyObject * delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, PyObject *microseconds, PyObject *milliseconds, PyObject *minutes, PyObject *hours, PyObject *weeks) -/*[clinic end generated code: output=61d7e02a92a97700 input=e8cd54819295d34b]*/ +/*[clinic end generated code: output=61d7e02a92a97700 input=8aebc4cdef360d1f]*/ { PyObject *self = NULL; @@ -3181,6 +3182,7 @@ static PyTypeObject PyDateTime_DeltaType = { 0, /* tp_init */ 0, /* tp_alloc */ delta_new, /* tp_new */ + .tp_vectorcall = delta_vectorcall, 0, /* tp_free */ }; @@ -4385,6 +4387,7 @@ static PyTypeObject PyDateTime_TZInfoType = { }; /*[clinic input] +@vectorcall @classmethod datetime.timezone.__new__ as timezone_new @@ -4396,7 +4399,7 @@ Fixed offset from UTC implementation of tzinfo. static PyObject * timezone_new_impl(PyTypeObject *type, PyObject *offset, PyObject *name) -/*[clinic end generated code: output=41a2dda500424187 input=d51255afe60382cd]*/ +/*[clinic end generated code: output=41a2dda500424187 input=8901a8befbc52561]*/ { return new_timezone(offset, name); } @@ -4641,6 +4644,7 @@ static PyTypeObject PyDateTime_TimeZoneType = { 0, /* tp_init */ 0, /* tp_alloc */ timezone_new, /* tp_new */ + .tp_vectorcall = timezone_vectorcall, }; // XXX Can we make this const? diff --git a/Modules/clinic/_datetimemodule.c.h b/Modules/clinic/_datetimemodule.c.h index 392a9da93ddbb41..9ad3a3e747ead5f 100644 --- a/Modules/clinic/_datetimemodule.c.h +++ b/Modules/clinic/_datetimemodule.c.h @@ -25,7 +25,8 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, PyObject *minutes, PyObject *hours, PyObject *weeks); static PyObject * -delta_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +delta_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -57,8 +58,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[7]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; + Py_ssize_t noptargs = nargs + nkw - 0; PyObject *days = NULL; PyObject *seconds = NULL; PyObject *microseconds = NULL; @@ -67,7 +67,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) PyObject *hours = NULL; PyObject *weeks = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 0, /*maxpos*/ 7, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -119,6 +119,72 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +delta_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return delta_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +delta_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *days = NULL; + PyObject *seconds = NULL; + PyObject *microseconds = NULL; + PyObject *milliseconds = NULL; + PyObject *minutes = NULL; + PyObject *hours = NULL; + PyObject *weeks = NULL; + + assert(_PyType_CAST(type)->tp_new == delta_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs > 7) { + return delta_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + if (nargs < 1) { + goto skip_optional; + } + days = args[0]; + if (nargs < 2) { + goto skip_optional; + } + seconds = args[1]; + if (nargs < 3) { + goto skip_optional; + } + microseconds = args[2]; + if (nargs < 4) { + goto skip_optional; + } + milliseconds = args[3]; + if (nargs < 5) { + goto skip_optional; + } + minutes = args[4]; + if (nargs < 6) { + goto skip_optional; + } + hours = args[5]; + if (nargs < 7) { + goto skip_optional; + } + weeks = args[6]; +skip_optional: + return_value = delta_new_impl(_PyType_CAST(type), days, seconds, microseconds, milliseconds, minutes, hours, weeks); + + return return_value; +} + PyDoc_STRVAR(datetime_date__doc__, "date(year, month, day)\n" "--\n" @@ -668,7 +734,8 @@ static PyObject * timezone_new_impl(PyTypeObject *type, PyObject *offset, PyObject *name); static PyObject * -timezone_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +timezone_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -700,12 +767,11 @@ timezone_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *offset; PyObject *name = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -730,6 +796,53 @@ timezone_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +timezone_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return timezone_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +timezone_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *offset; + PyObject *name = NULL; + + assert(_PyType_CAST(type)->tp_new == timezone_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 2) { + return timezone_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + if (!PyObject_TypeCheck(args[0], DELTA_TYPE(NO_STATE))) { + _PyArg_BadArgument("timezone", "argument 'offset'", (DELTA_TYPE(NO_STATE))->tp_name, args[0]); + goto exit; + } + offset = args[0]; + if (nargs < 2) { + goto skip_optional; + } + if (!PyUnicode_Check(args[1])) { + _PyArg_BadArgument("timezone", "argument 'name'", "str", args[1]); + goto exit; + } + name = args[1]; +skip_optional: + return_value = timezone_new_impl(_PyType_CAST(type), offset, name); + +exit: + return return_value; +} + PyDoc_STRVAR(datetime_time__doc__, "time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)\n" "--\n" @@ -2082,4 +2195,4 @@ datetime_datetime___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) { return datetime_datetime___reduce___impl((PyDateTime_DateTime *)self); } -/*[clinic end generated code: output=7bdfdfbd568c63ed input=a9049054013a1b77]*/ +/*[clinic end generated code: output=380121f4e088e032 input=a9049054013a1b77]*/ From cd47f779b4fc2d62dc7ad79269431b45bba0a26a Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 26 Nov 2026 00:22:37 +0800 Subject: [PATCH 2/2] move .tp_vectorcall to the end --- Modules/_datetimemodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index ce8babb01821d93..da1e36f3da03dd5 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -3182,8 +3182,8 @@ static PyTypeObject PyDateTime_DeltaType = { 0, /* tp_init */ 0, /* tp_alloc */ delta_new, /* tp_new */ - .tp_vectorcall = delta_vectorcall, 0, /* tp_free */ + .tp_vectorcall = delta_vectorcall, }; // XXX Can we make this const?