mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-93274: Make vectorcall safe on mutable classes & inherit it by default (#95437)
This commit is contained in:
parent
a613fedd6e
commit
7b370b7305
8 changed files with 351 additions and 21 deletions
107
Modules/_testcapi/clinic/vectorcall.c.h
generated
Normal file
107
Modules/_testcapi/clinic/vectorcall.c.h
generated
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*[clinic input]
|
||||
preserve
|
||||
[clinic start generated code]*/
|
||||
|
||||
PyDoc_STRVAR(_testcapi_VectorCallClass_set_vectorcall__doc__,
|
||||
"set_vectorcall($self, type, /)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Set self\'s vectorcall function for `type` to one that returns \"vectorcall\"");
|
||||
|
||||
#define _TESTCAPI_VECTORCALLCLASS_SET_VECTORCALL_METHODDEF \
|
||||
{"set_vectorcall", (PyCFunction)_testcapi_VectorCallClass_set_vectorcall, METH_O, _testcapi_VectorCallClass_set_vectorcall__doc__},
|
||||
|
||||
static PyObject *
|
||||
_testcapi_VectorCallClass_set_vectorcall_impl(PyObject *self,
|
||||
PyTypeObject *type);
|
||||
|
||||
static PyObject *
|
||||
_testcapi_VectorCallClass_set_vectorcall(PyObject *self, PyObject *arg)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
PyTypeObject *type;
|
||||
|
||||
if (!PyObject_TypeCheck(arg, &PyType_Type)) {
|
||||
_PyArg_BadArgument("set_vectorcall", "argument", (&PyType_Type)->tp_name, arg);
|
||||
goto exit;
|
||||
}
|
||||
type = (PyTypeObject *)arg;
|
||||
return_value = _testcapi_VectorCallClass_set_vectorcall_impl(self, type);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(_testcapi_make_vectorcall_class__doc__,
|
||||
"make_vectorcall_class($module, base=<unrepresentable>, /)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Create a class whose instances return \"tpcall\" when called.\n"
|
||||
"\n"
|
||||
"When the \"set_vectorcall\" method is called on an instance, a vectorcall\n"
|
||||
"function that returns \"vectorcall\" will be installed.");
|
||||
|
||||
#define _TESTCAPI_MAKE_VECTORCALL_CLASS_METHODDEF \
|
||||
{"make_vectorcall_class", _PyCFunction_CAST(_testcapi_make_vectorcall_class), METH_FASTCALL, _testcapi_make_vectorcall_class__doc__},
|
||||
|
||||
static PyObject *
|
||||
_testcapi_make_vectorcall_class_impl(PyObject *module, PyTypeObject *base);
|
||||
|
||||
static PyObject *
|
||||
_testcapi_make_vectorcall_class(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
PyTypeObject *base = NULL;
|
||||
|
||||
if (!_PyArg_CheckPositional("make_vectorcall_class", nargs, 0, 1)) {
|
||||
goto exit;
|
||||
}
|
||||
if (nargs < 1) {
|
||||
goto skip_optional;
|
||||
}
|
||||
if (!PyObject_TypeCheck(args[0], &PyType_Type)) {
|
||||
_PyArg_BadArgument("make_vectorcall_class", "argument 1", (&PyType_Type)->tp_name, args[0]);
|
||||
goto exit;
|
||||
}
|
||||
base = (PyTypeObject *)args[0];
|
||||
skip_optional:
|
||||
return_value = _testcapi_make_vectorcall_class_impl(module, base);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(_testcapi_has_vectorcall_flag__doc__,
|
||||
"has_vectorcall_flag($module, type, /)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Return true iff Py_TPFLAGS_HAVE_VECTORCALL is set on the class.");
|
||||
|
||||
#define _TESTCAPI_HAS_VECTORCALL_FLAG_METHODDEF \
|
||||
{"has_vectorcall_flag", (PyCFunction)_testcapi_has_vectorcall_flag, METH_O, _testcapi_has_vectorcall_flag__doc__},
|
||||
|
||||
static int
|
||||
_testcapi_has_vectorcall_flag_impl(PyObject *module, PyTypeObject *type);
|
||||
|
||||
static PyObject *
|
||||
_testcapi_has_vectorcall_flag(PyObject *module, PyObject *arg)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
PyTypeObject *type;
|
||||
int _return_value;
|
||||
|
||||
if (!PyObject_TypeCheck(arg, &PyType_Type)) {
|
||||
_PyArg_BadArgument("has_vectorcall_flag", "argument", (&PyType_Type)->tp_name, arg);
|
||||
goto exit;
|
||||
}
|
||||
type = (PyTypeObject *)arg;
|
||||
_return_value = _testcapi_has_vectorcall_flag_impl(module, type);
|
||||
if ((_return_value == -1) && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
return_value = PyBool_FromLong((long)_return_value);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
/*[clinic end generated code: output=cf39927be151aebd input=a9049054013a1b77]*/
|
|
@ -1,5 +1,8 @@
|
|||
#include "parts.h"
|
||||
#include <stddef.h> // offsetof
|
||||
#include "clinic/vectorcall.c.h"
|
||||
|
||||
#include "structmember.h" // PyMemberDef
|
||||
#include <stddef.h> // offsetof
|
||||
|
||||
|
||||
/* Test PEP 590 - Vectorcall */
|
||||
|
@ -122,11 +125,128 @@ test_pyvectorcall_call(PyObject *self, PyObject *args)
|
|||
return PyVectorcall_Call(func, argstuple, kwargs);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
VectorCallClass_tpcall(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
return PyUnicode_FromString("tp_call");
|
||||
}
|
||||
|
||||
PyObject *
|
||||
VectorCallClass_vectorcall(PyObject *callable,
|
||||
PyObject *const *args,
|
||||
size_t nargsf,
|
||||
PyObject *kwnames) {
|
||||
return PyUnicode_FromString("vectorcall");
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
module _testcapi
|
||||
class _testcapi.VectorCallClass "PyObject *" "&PyType_Type"
|
||||
[clinic start generated code]*/
|
||||
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8423a8e919f2f0df]*/
|
||||
|
||||
/*[clinic input]
|
||||
_testcapi.VectorCallClass.set_vectorcall
|
||||
|
||||
type: object(subclass_of="&PyType_Type", type="PyTypeObject *")
|
||||
/
|
||||
|
||||
Set self's vectorcall function for `type` to one that returns "vectorcall"
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_testcapi_VectorCallClass_set_vectorcall_impl(PyObject *self,
|
||||
PyTypeObject *type)
|
||||
/*[clinic end generated code: output=b37f0466f15da903 input=840de66182c7d71a]*/
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, type)) {
|
||||
return PyErr_Format(
|
||||
PyExc_TypeError,
|
||||
"expected %s instance",
|
||||
PyType_GetName(type));
|
||||
}
|
||||
if (!type->tp_vectorcall_offset) {
|
||||
return PyErr_Format(
|
||||
PyExc_TypeError,
|
||||
"type %s has no vectorcall offset",
|
||||
PyType_GetName(type));
|
||||
}
|
||||
*(vectorcallfunc*)((char*)self + type->tp_vectorcall_offset) = (
|
||||
VectorCallClass_vectorcall);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyMethodDef VectorCallClass_methods[] = {
|
||||
_TESTCAPI_VECTORCALLCLASS_SET_VECTORCALL_METHODDEF
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
PyMemberDef VectorCallClass_members[] = {
|
||||
{"__vectorcalloffset__", T_PYSSIZET, 0/* set later */, READONLY},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
PyType_Slot VectorCallClass_slots[] = {
|
||||
{Py_tp_call, VectorCallClass_tpcall},
|
||||
{Py_tp_members, VectorCallClass_members},
|
||||
{Py_tp_methods, VectorCallClass_methods},
|
||||
{0},
|
||||
};
|
||||
|
||||
/*[clinic input]
|
||||
_testcapi.make_vectorcall_class
|
||||
|
||||
base: object(subclass_of="&PyType_Type", type="PyTypeObject *") = NULL
|
||||
/
|
||||
|
||||
Create a class whose instances return "tpcall" when called.
|
||||
|
||||
When the "set_vectorcall" method is called on an instance, a vectorcall
|
||||
function that returns "vectorcall" will be installed.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_testcapi_make_vectorcall_class_impl(PyObject *module, PyTypeObject *base)
|
||||
/*[clinic end generated code: output=16dcfc3062ddf968 input=f72e01ccf52de2b4]*/
|
||||
{
|
||||
if (!base) {
|
||||
base = (PyTypeObject *)&PyBaseObject_Type;
|
||||
}
|
||||
VectorCallClass_members[0].offset = base->tp_basicsize;
|
||||
PyType_Spec spec = {
|
||||
.name = "_testcapi.VectorcallClass",
|
||||
.basicsize = base->tp_basicsize + (int)sizeof(vectorcallfunc),
|
||||
.flags = Py_TPFLAGS_DEFAULT
|
||||
| Py_TPFLAGS_HAVE_VECTORCALL
|
||||
| Py_TPFLAGS_BASETYPE,
|
||||
.slots = VectorCallClass_slots,
|
||||
};
|
||||
|
||||
return PyType_FromSpecWithBases(&spec, (PyObject *)base);
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
_testcapi.has_vectorcall_flag -> bool
|
||||
|
||||
type: object(subclass_of="&PyType_Type", type="PyTypeObject *")
|
||||
/
|
||||
|
||||
Return true iff Py_TPFLAGS_HAVE_VECTORCALL is set on the class.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static int
|
||||
_testcapi_has_vectorcall_flag_impl(PyObject *module, PyTypeObject *type)
|
||||
/*[clinic end generated code: output=3ae8d1374388c671 input=8eee492ac548749e]*/
|
||||
{
|
||||
return PyType_HasFeature(type, Py_TPFLAGS_HAVE_VECTORCALL);
|
||||
}
|
||||
|
||||
static PyMethodDef TestMethods[] = {
|
||||
{"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS},
|
||||
{"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
|
||||
{"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS},
|
||||
{"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS},
|
||||
_TESTCAPI_MAKE_VECTORCALL_CLASS_METHODDEF
|
||||
_TESTCAPI_HAS_VECTORCALL_FLAG_METHODDEF
|
||||
{NULL},
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue