summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorJeroen Demeyer <J.Demeyer@UGent.be>2019-05-30 10:43:19 (GMT)
committerPetr Viktorin <encukou@gmail.com>2019-05-30 10:43:19 (GMT)
commit735e8afa9ee942367b5d0807633a2b9f662cbdbf (patch)
tree5ec70a74c5399a91fb78183139ca67d3950ba4d6 /Modules
parent0f39c2b1919727904f4fac2d79cb41dc6bfe41fe (diff)
downloadcpython-735e8afa9ee942367b5d0807633a2b9f662cbdbf.zip
cpython-735e8afa9ee942367b5d0807633a2b9f662cbdbf.tar.gz
cpython-735e8afa9ee942367b5d0807633a2b9f662cbdbf.tar.bz2
bpo-36974: inherit the vectorcall protocol (GH-13498)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c68
1 files changed, 67 insertions, 1 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f2f418c..a7451c6 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -5814,6 +5814,29 @@ static PyTypeObject Generic_Type = {
/* Test PEP 590 */
+typedef struct {
+ PyObject_HEAD
+ vectorcallfunc vectorcall;
+} MethodDescriptorObject;
+
+static PyObject *
+MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ /* True if using the vectorcall function in MethodDescriptorObject
+ * but False for MethodDescriptor2Object */
+ MethodDescriptorObject *md = (MethodDescriptorObject *)callable;
+ return PyBool_FromLong(md->vectorcall != NULL);
+}
+
+static PyObject *
+MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw)
+{
+ MethodDescriptorObject *op = PyObject_New(MethodDescriptorObject, type);
+ op->vectorcall = MethodDescriptor_vectorcall;
+ return (PyObject *)op;
+}
+
static PyObject *
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
{
@@ -5831,10 +5854,22 @@ nop_descr_get(PyObject *func, PyObject *obj, PyObject *type)
return func;
}
+static PyObject *
+call_return_args(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ Py_INCREF(args);
+ return args;
+}
+
static PyTypeObject MethodDescriptorBase_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"MethodDescriptorBase",
- .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_METHOD_DESCRIPTOR,
+ sizeof(MethodDescriptorObject),
+ .tp_new = MethodDescriptor_new,
+ .tp_call = PyVectorcall_Call,
+ .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall),
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_METHOD_DESCRIPTOR | _Py_TPFLAGS_HAVE_VECTORCALL,
.tp_descr_get = func_descr_get,
};
@@ -5848,9 +5883,34 @@ static PyTypeObject MethodDescriptorNopGet_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"MethodDescriptorNopGet",
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ .tp_call = call_return_args,
.tp_descr_get = nop_descr_get,
};
+typedef struct {
+ MethodDescriptorObject base;
+ vectorcallfunc vectorcall;
+} MethodDescriptor2Object;
+
+static PyObject *
+MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw)
+{
+ MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type);
+ op->base.vectorcall = NULL;
+ op->vectorcall = MethodDescriptor_vectorcall;
+ return (PyObject *)op;
+}
+
+static PyTypeObject MethodDescriptor2_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "MethodDescriptor2",
+ sizeof(MethodDescriptor2Object),
+ .tp_new = MethodDescriptor2_new,
+ .tp_call = PyVectorcall_Call,
+ .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall),
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | _Py_TPFLAGS_HAVE_VECTORCALL,
+};
+
static struct PyModuleDef _testcapimodule = {
PyModuleDef_HEAD_INIT,
@@ -5916,6 +5976,12 @@ PyInit__testcapi(void)
Py_INCREF(&MethodDescriptorNopGet_Type);
PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type);
+ MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type;
+ if (PyType_Ready(&MethodDescriptor2_Type) < 0)
+ return NULL;
+ Py_INCREF(&MethodDescriptor2_Type);
+ PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type);
+
if (PyType_Ready(&GenericAlias_Type) < 0)
return NULL;
Py_INCREF(&GenericAlias_Type);