diff --git a/Objects/classobject.c b/Objects/classobject.c index 238f1c1dad7d866..dbfdc9262cf0202 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -182,8 +182,9 @@ method_getattro(PyObject *obj, PyObject *name) } /*[clinic input] +@vectorcall @classmethod -method.__new__ as method_new +method.__new__ as method_type_new function: object instance: object / @@ -192,8 +193,9 @@ Create a bound instance method object. [clinic start generated code]*/ static PyObject * -method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance) -/*[clinic end generated code: output=d33ef4ebf702e1f7 input=4e32facc3c3108ae]*/ +method_type_new_impl(PyTypeObject *type, PyObject *function, + PyObject *instance) +/*[clinic end generated code: output=e9877864a8bfdf70 input=70c1e0da5d6267f5]*/ { if (!PyCallable_Check(function)) { PyErr_SetString(PyExc_TypeError, @@ -323,7 +325,7 @@ PyTypeObject PyMethod_Type = { .tp_setattro = PyObject_GenericSetAttr, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_VECTORCALL, - .tp_doc = method_new__doc__, + .tp_doc = method_type_new__doc__, .tp_traverse = method_traverse, .tp_richcompare = method_richcompare, .tp_weaklistoffset = offsetof(PyMethodObject, im_weakreflist), @@ -331,7 +333,8 @@ PyTypeObject PyMethod_Type = { .tp_members = method_memberlist, .tp_getset = method_getset, .tp_descr_get = method_descr_get, - .tp_new = method_new, + .tp_new = method_type_new, + .tp_vectorcall = method_type_vectorcall, }; /* ------------------------------------------------------------------------ diff --git a/Objects/clinic/classobject.c.h b/Objects/clinic/classobject.c.h index 5934f1c2a41669f..1b96cc2212a826f 100644 --- a/Objects/clinic/classobject.c.h +++ b/Objects/clinic/classobject.c.h @@ -21,17 +21,18 @@ method___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) return method___reduce___impl((PyMethodObject *)self); } -PyDoc_STRVAR(method_new__doc__, +PyDoc_STRVAR(method_type_new__doc__, "method(function, instance, /)\n" "--\n" "\n" "Create a bound instance method object."); static PyObject * -method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance); +method_type_new_impl(PyTypeObject *type, PyObject *function, + PyObject *instance); static PyObject * -method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +method_type_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; PyTypeObject *base_tp = &PyMethod_Type; @@ -47,7 +48,34 @@ method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) } function = PyTuple_GET_ITEM(args, 0); instance = PyTuple_GET_ITEM(args, 1); - return_value = method_new_impl(type, function, instance); + return_value = method_type_new_impl(type, function, instance); + +exit: + return return_value; +} + +static PyObject * +method_type_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *function; + PyObject *instance; + + assert(_PyType_CAST(type)->tp_new == method_type_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 (!_PyArg_NoKwnames("method", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("method", nargs, 2, 2)) { + goto exit; + } + function = args[0]; + instance = args[1]; + return_value = method_type_new_impl(_PyType_CAST(type), function, instance); exit: return return_value; @@ -82,4 +110,4 @@ instancemethod_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=ab546abf90aac94e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fd5c214ee6e55e88 input=a9049054013a1b77]*/