blob: 7b430416c5a048715c1e8e91b4128c46ca25f828 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Method object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02005#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
Victor Stinnerbcda8f12018-11-21 22:27:47 +01006#include "pycore_object.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01007#include "pycore_pyerrors.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02008#include "pycore_pystate.h" // _PyThreadState_GET()
9#include "structmember.h" // PyMemberDef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Andrew Svetlov4de29242012-12-26 23:08:54 +020011/* undefine macro trampoline to PyCFunction_NewEx */
12#undef PyCFunction_New
Petr Viktorine1becf42020-05-07 15:39:59 +020013/* undefine macro trampoline to PyCMethod_New */
14#undef PyCFunction_NewEx
Andrew Svetlov4de29242012-12-26 23:08:54 +020015
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020016/* Forward declarations */
17static PyObject * cfunction_vectorcall_FASTCALL(
18 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
19static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
20 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
Petr Viktorine1becf42020-05-07 15:39:59 +020021static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
22 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020023static PyObject * cfunction_vectorcall_NOARGS(
24 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
25static PyObject * cfunction_vectorcall_O(
26 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +020027static PyObject * cfunction_call(
28 PyObject *func, PyObject *args, PyObject *kwargs);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020029
30
Benjamin Petersone5024512018-09-12 12:06:42 -070031PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020032PyCFunction_New(PyMethodDef *ml, PyObject *self)
33{
34 return PyCFunction_NewEx(ml, self, NULL);
35}
36
37PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000038PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039{
Petr Viktorine1becf42020-05-07 15:39:59 +020040 return PyCMethod_New(ml, self, module, NULL);
41}
42
43PyObject *
44PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *cls)
45{
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020046 /* Figure out correct vectorcall function to use */
47 vectorcallfunc vectorcall;
Petr Viktorine1becf42020-05-07 15:39:59 +020048 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS |
49 METH_O | METH_KEYWORDS | METH_METHOD))
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020050 {
51 case METH_VARARGS:
52 case METH_VARARGS | METH_KEYWORDS:
53 /* For METH_VARARGS functions, it's more efficient to use tp_call
54 * instead of vectorcall. */
55 vectorcall = NULL;
56 break;
57 case METH_FASTCALL:
58 vectorcall = cfunction_vectorcall_FASTCALL;
59 break;
60 case METH_FASTCALL | METH_KEYWORDS:
61 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
62 break;
63 case METH_NOARGS:
64 vectorcall = cfunction_vectorcall_NOARGS;
65 break;
66 case METH_O:
67 vectorcall = cfunction_vectorcall_O;
68 break;
Petr Viktorine1becf42020-05-07 15:39:59 +020069 case METH_METHOD | METH_FASTCALL | METH_KEYWORDS:
70 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD;
71 break;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020072 default:
Victor Stinnerc7d2d692020-03-12 08:38:11 +010073 PyErr_Format(PyExc_SystemError,
74 "%s() method: bad call flags", ml->ml_name);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020075 return NULL;
76 }
77
Petr Viktorine1becf42020-05-07 15:39:59 +020078 PyCFunctionObject *op = NULL;
79
80 if (ml->ml_flags & METH_METHOD) {
81 if (!cls) {
82 PyErr_SetString(PyExc_SystemError,
83 "attempting to create PyCMethod with a METH_METHOD "
84 "flag but no class");
85 return NULL;
86 }
87 PyCMethodObject *om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
88 if (om == NULL) {
89 return NULL;
90 }
91 Py_INCREF(cls);
92 om->mm_class = cls;
93 op = (PyCFunctionObject *)om;
94 } else {
95 if (cls) {
96 PyErr_SetString(PyExc_SystemError,
97 "attempting to create PyCFunction with class "
98 "but no METH_METHOD flag");
99 return NULL;
100 }
101 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
102 if (op == NULL) {
103 return NULL;
104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Petr Viktorine1becf42020-05-07 15:39:59 +0200106
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400107 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 op->m_ml = ml;
109 Py_XINCREF(self);
110 op->m_self = self;
111 Py_XINCREF(module);
112 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200113 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 _PyObject_GC_TRACK(op);
115 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116}
117
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +0000119PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (!PyCFunction_Check(op)) {
122 PyErr_BadInternalCall();
123 return NULL;
124 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100125 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126}
127
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000129PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 if (!PyCFunction_Check(op)) {
132 PyErr_BadInternalCall();
133 return NULL;
134 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100135 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossumc0602291991-12-16 13:07:24 +0000138int
Fred Drakeee238b92000-07-09 06:03:25 +0000139PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +0000140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (!PyCFunction_Check(op)) {
142 PyErr_BadInternalCall();
143 return -1;
144 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100145 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000146}
147
Petr Viktorine1becf42020-05-07 15:39:59 +0200148PyTypeObject *
149PyCMethod_GetClass(PyObject *op)
150{
151 if (!PyCFunction_Check(op)) {
152 PyErr_BadInternalCall();
153 return NULL;
154 }
155 return PyCFunction_GET_CLASS(op);
156}
157
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158/* Methods (the standard built-in methods, that is) */
159
160static void
Fred Drakeee238b92000-07-09 06:03:25 +0000161meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400164 if (m->m_weakreflist != NULL) {
165 PyObject_ClearWeakRefs((PyObject*) m);
166 }
Yannick Jadoul04b86312020-10-12 23:06:19 +0200167 // Dereference class before m_self: PyCFunction_GET_CLASS accesses
168 // PyMethodDef m_ml, which could be kept alive by m_self
169 Py_XDECREF(PyCFunction_GET_CLASS(m));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 Py_XDECREF(m->m_self);
171 Py_XDECREF(m->m_module);
Inada Naoki3e54b572019-07-26 15:05:50 +0900172 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173}
174
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800175static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530176meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800177{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800178 _Py_IDENTIFIER(getattr);
179
180 if (m->m_self == NULL || PyModule_Check(m->m_self))
181 return PyUnicode_FromString(m->m_ml->ml_name);
182
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200183 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
184 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800185}
186
187static PyMethodDef meth_methods[] = {
188 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
189 {NULL, NULL}
190};
191
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800192static PyObject *
193meth_get__text_signature__(PyCFunctionObject *m, void *closure)
194{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800195 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800196}
197
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198static PyObject *
199meth_get__doc__(PyCFunctionObject *m, void *closure)
200{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800201 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000202}
203
204static PyObject *
205meth_get__name__(PyCFunctionObject *m, void *closure)
206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000208}
209
Antoine Pitrou5b629422011-12-23 12:40:16 +0100210static PyObject *
211meth_get__qualname__(PyCFunctionObject *m, void *closure)
212{
213 /* If __self__ is a module or NULL, return m.__name__
214 (e.g. len.__qualname__ == 'len')
215
216 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
217 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
218
219 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
220 (e.g. [].append.__qualname__ == 'list.append') */
221 PyObject *type, *type_qualname, *res;
222 _Py_IDENTIFIER(__qualname__);
223
224 if (m->m_self == NULL || PyModule_Check(m->m_self))
225 return PyUnicode_FromString(m->m_ml->ml_name);
226
227 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
228
229 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
230 if (type_qualname == NULL)
231 return NULL;
232
233 if (!PyUnicode_Check(type_qualname)) {
234 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
235 "__qualname__ is not a unicode object");
236 Py_XDECREF(type_qualname);
237 return NULL;
238 }
239
240 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
241 Py_DECREF(type_qualname);
242 return res;
243}
244
Neil Schemenauer10c66922001-07-12 13:27:35 +0000245static int
246meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
247{
Yannick Jadoul04b86312020-10-12 23:06:19 +0200248 Py_VISIT(PyCFunction_GET_CLASS(m));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_VISIT(m->m_self);
250 Py_VISIT(m->m_module);
251 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000252}
253
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000255meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000258
Antoine Pitrou5b629422011-12-23 12:40:16 +0100259 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (self == NULL)
261 self = Py_None;
262 Py_INCREF(self);
263 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000264}
265
Guido van Rossum32d34c82001-09-20 21:45:26 +0000266static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
268 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100269 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800271 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000273};
274
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000275#define OFF(x) offsetof(PyCFunctionObject, x)
276
277static PyMemberDef meth_members[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100278 {"__module__", T_OBJECT, OFF(m_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000280};
281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000283meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (m->m_self == NULL || PyModule_Check(m->m_self))
286 return PyUnicode_FromFormat("<built-in function %s>",
287 m->m_ml->ml_name);
288 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
289 m->m_ml->ml_name,
Victor Stinner58ac7002020-02-07 03:04:21 +0100290 Py_TYPE(m->m_self)->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292}
293
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000294static PyObject *
295meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyCFunctionObject *a, *b;
298 PyObject *res;
299 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if ((op != Py_EQ && op != Py_NE) ||
302 !PyCFunction_Check(self) ||
303 !PyCFunction_Check(other))
304 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500305 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 }
307 a = (PyCFunctionObject *)self;
308 b = (PyCFunctionObject *)other;
309 eq = a->m_self == b->m_self;
310 if (eq)
311 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
312 if (op == Py_EQ)
313 res = eq ? Py_True : Py_False;
314 else
315 res = eq ? Py_False : Py_True;
316 Py_INCREF(res);
317 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000318}
319
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000320static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000321meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000322{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000323 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300324 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 x ^= y;
327 if (x == -1)
328 x = -2;
329 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000330}
331
Tim Peters6d6c1a32001-08-02 04:15:00 +0000332
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 PyVarObject_HEAD_INIT(&PyType_Type, 0)
335 "builtin_function_or_method",
336 sizeof(PyCFunctionObject),
337 0,
338 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200339 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 0, /* tp_getattr */
341 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200342 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 (reprfunc)meth_repr, /* tp_repr */
344 0, /* tp_as_number */
345 0, /* tp_as_sequence */
346 0, /* tp_as_mapping */
347 (hashfunc)meth_hash, /* tp_hash */
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200348 cfunction_call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 0, /* tp_str */
350 PyObject_GenericGetAttr, /* tp_getattro */
351 0, /* tp_setattro */
352 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200353 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100354 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 0, /* tp_doc */
356 (traverseproc)meth_traverse, /* tp_traverse */
357 0, /* tp_clear */
358 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400359 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 0, /* tp_iter */
361 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800362 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 meth_members, /* tp_members */
364 meth_getsets, /* tp_getset */
365 0, /* tp_base */
366 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000368
Petr Viktorine1becf42020-05-07 15:39:59 +0200369PyTypeObject PyCMethod_Type = {
370 PyVarObject_HEAD_INIT(&PyType_Type, 0)
371 .tp_name = "builtin_method",
372 .tp_basicsize = sizeof(PyCMethodObject),
373 .tp_base = &PyCFunction_Type,
374};
375
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200376/* Vectorcall functions for each of the PyCFunction calling conventions,
377 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
378 * doesn't use vectorcall.
379 *
380 * First, common helpers
381 */
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200382
383static inline int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100384cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200385{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100386 assert(!_PyErr_Occurred(tstate));
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200387 assert(PyCFunction_Check(func));
388 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100389 PyObject *funcstr = _PyObject_FunctionStr(func);
390 if (funcstr != NULL) {
391 _PyErr_Format(tstate, PyExc_TypeError,
392 "%U takes no keyword arguments", funcstr);
393 Py_DECREF(funcstr);
394 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200395 return -1;
396 }
397 return 0;
398}
399
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100400typedef void (*funcptr)(void);
401
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200402static inline funcptr
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100403cfunction_enter_call(PyThreadState *tstate, PyObject *func)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200404{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100405 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200406 return NULL;
407 }
408 return (funcptr)PyCFunction_GET_FUNCTION(func);
409}
410
411/* Now the actual vectorcall functions */
412static PyObject *
413cfunction_vectorcall_FASTCALL(
414 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
415{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100416 PyThreadState *tstate = _PyThreadState_GET();
417 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200418 return NULL;
419 }
420 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
421 _PyCFunctionFast meth = (_PyCFunctionFast)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100422 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200423 if (meth == NULL) {
424 return NULL;
425 }
426 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100427 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200428 return result;
429}
430
431static PyObject *
432cfunction_vectorcall_FASTCALL_KEYWORDS(
433 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
434{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100435 PyThreadState *tstate = _PyThreadState_GET();
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200436 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
437 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100438 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200439 if (meth == NULL) {
440 return NULL;
441 }
442 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100443 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200444 return result;
445}
446
447static PyObject *
Petr Viktorine1becf42020-05-07 15:39:59 +0200448cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
449 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
450{
451 PyThreadState *tstate = _PyThreadState_GET();
452 PyTypeObject *cls = PyCFunction_GET_CLASS(func);
453 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
454 PyCMethod meth = (PyCMethod)cfunction_enter_call(tstate, func);
455 if (meth == NULL) {
456 return NULL;
457 }
458 PyObject *result = meth(PyCFunction_GET_SELF(func), cls, args, nargs, kwnames);
459 _Py_LeaveRecursiveCall(tstate);
460 return result;
461}
462
463static PyObject *
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200464cfunction_vectorcall_NOARGS(
465 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
466{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100467 PyThreadState *tstate = _PyThreadState_GET();
468 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200469 return NULL;
470 }
471 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
472 if (nargs != 0) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100473 PyObject *funcstr = _PyObject_FunctionStr(func);
474 if (funcstr != NULL) {
475 _PyErr_Format(tstate, PyExc_TypeError,
476 "%U takes no arguments (%zd given)", funcstr, nargs);
477 Py_DECREF(funcstr);
478 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200479 return NULL;
480 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100481 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200482 if (meth == NULL) {
483 return NULL;
484 }
485 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100486 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200487 return result;
488}
489
490static PyObject *
491cfunction_vectorcall_O(
492 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
493{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100494 PyThreadState *tstate = _PyThreadState_GET();
495 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200496 return NULL;
497 }
498 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
499 if (nargs != 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100500 PyObject *funcstr = _PyObject_FunctionStr(func);
501 if (funcstr != NULL) {
502 _PyErr_Format(tstate, PyExc_TypeError,
503 "%U takes exactly one argument (%zd given)", funcstr, nargs);
504 Py_DECREF(funcstr);
505 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200506 return NULL;
507 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100508 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200509 if (meth == NULL) {
510 return NULL;
511 }
512 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100513 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200514 return result;
515}
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200516
517
518static PyObject *
519cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
520{
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200521 assert(kwargs == NULL || PyDict_Check(kwargs));
522
Victor Stinner17269092019-11-05 01:22:12 +0100523 PyThreadState *tstate = _PyThreadState_GET();
524 assert(!_PyErr_Occurred(tstate));
525
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200526 int flags = PyCFunction_GET_FLAGS(func);
527 if (!(flags & METH_VARARGS)) {
528 /* If this is not a METH_VARARGS function, delegate to vectorcall */
529 return PyVectorcall_Call(func, args, kwargs);
530 }
531
532 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
533 * is NULL. This is intentional, since vectorcall would be slower. */
534 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
535 PyObject *self = PyCFunction_GET_SELF(func);
536
537 PyObject *result;
538 if (flags & METH_KEYWORDS) {
539 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
540 }
541 else {
542 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner17269092019-11-05 01:22:12 +0100543 _PyErr_Format(tstate, PyExc_TypeError,
544 "%.200s() takes no keyword arguments",
545 ((PyCFunctionObject*)func)->m_ml->ml_name);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200546 return NULL;
547 }
548 result = meth(self, args);
549 }
Victor Stinner17269092019-11-05 01:22:12 +0100550 return _Py_CheckFunctionResult(tstate, func, result, NULL);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200551}