GH-91049: Introduce set vectorcall field API for PyFunctionObject (GH-92257)

Co-authored-by: Andrew Frost <adfrost@fb.com>
Co-authored-by: Itamar Ostricher <itamarost@gmail.com>
This commit is contained in:
adphrost 2022-09-15 08:42:37 -07:00 committed by GitHub
parent e37ac5fbb6
commit a41ed975e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 115 additions and 3 deletions

View file

@ -102,6 +102,24 @@ test_pyobject_vectorcall(PyObject *self, PyObject *args)
return PyObject_Vectorcall(func, stack, nargs, kwnames);
}
static PyObject *
override_vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf,
PyObject *kwnames)
{
return PyUnicode_FromString("overridden");
}
static PyObject *
function_setvectorcall(PyObject *self, PyObject *func)
{
if (!PyFunction_Check(func)) {
PyErr_SetString(PyExc_TypeError, "'func' must be a function");
return NULL;
}
PyFunction_SetVectorcall((PyFunctionObject *)func, (vectorcallfunc)override_vectorcall);
Py_RETURN_NONE;
}
static PyObject *
test_pyvectorcall_call(PyObject *self, PyObject *args)
{
@ -244,6 +262,7 @@ static PyMethodDef TestMethods[] = {
{"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS},
{"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
{"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS},
{"function_setvectorcall", function_setvectorcall, METH_O},
{"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS},
_TESTCAPI_MAKE_VECTORCALL_CLASS_METHODDEF
_TESTCAPI_HAS_VECTORCALL_FLAG_METHODDEF