blob: 5659f2143d1823396d7be2d1e115cde21ee5e0a2 [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 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 Py_XDECREF(m->m_self);
168 Py_XDECREF(m->m_module);
Petr Viktorine1becf42020-05-07 15:39:59 +0200169 Py_XDECREF(PyCFunction_GET_CLASS(m));
Inada Naoki3e54b572019-07-26 15:05:50 +0900170 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800173static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530174meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800175{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800176 _Py_IDENTIFIER(getattr);
177
178 if (m->m_self == NULL || PyModule_Check(m->m_self))
179 return PyUnicode_FromString(m->m_ml->ml_name);
180
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200181 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
182 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800183}
184
185static PyMethodDef meth_methods[] = {
186 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
187 {NULL, NULL}
188};
189
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800190static PyObject *
191meth_get__text_signature__(PyCFunctionObject *m, void *closure)
192{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800193 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800194}
195
Tim Peters6d6c1a32001-08-02 04:15:00 +0000196static PyObject *
197meth_get__doc__(PyCFunctionObject *m, void *closure)
198{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800199 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200}
201
202static PyObject *
203meth_get__name__(PyCFunctionObject *m, void *closure)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206}
207
Antoine Pitrou5b629422011-12-23 12:40:16 +0100208static PyObject *
209meth_get__qualname__(PyCFunctionObject *m, void *closure)
210{
211 /* If __self__ is a module or NULL, return m.__name__
212 (e.g. len.__qualname__ == 'len')
213
214 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
215 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
216
217 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
218 (e.g. [].append.__qualname__ == 'list.append') */
219 PyObject *type, *type_qualname, *res;
220 _Py_IDENTIFIER(__qualname__);
221
222 if (m->m_self == NULL || PyModule_Check(m->m_self))
223 return PyUnicode_FromString(m->m_ml->ml_name);
224
225 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
226
227 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
228 if (type_qualname == NULL)
229 return NULL;
230
231 if (!PyUnicode_Check(type_qualname)) {
232 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
233 "__qualname__ is not a unicode object");
234 Py_XDECREF(type_qualname);
235 return NULL;
236 }
237
238 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
239 Py_DECREF(type_qualname);
240 return res;
241}
242
Neil Schemenauer10c66922001-07-12 13:27:35 +0000243static int
244meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 Py_VISIT(m->m_self);
247 Py_VISIT(m->m_module);
Petr Viktorine1becf42020-05-07 15:39:59 +0200248 Py_VISIT(PyCFunction_GET_CLASS(m));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000250}
251
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000253meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000256
Antoine Pitrou5b629422011-12-23 12:40:16 +0100257 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (self == NULL)
259 self = Py_None;
260 Py_INCREF(self);
261 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000262}
263
Guido van Rossum32d34c82001-09-20 21:45:26 +0000264static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
266 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100267 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800269 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000271};
272
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000273#define OFF(x) offsetof(PyCFunctionObject, x)
274
275static PyMemberDef meth_members[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100276 {"__module__", T_OBJECT, OFF(m_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000278};
279
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000281meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (m->m_self == NULL || PyModule_Check(m->m_self))
284 return PyUnicode_FromFormat("<built-in function %s>",
285 m->m_ml->ml_name);
286 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
287 m->m_ml->ml_name,
Victor Stinner58ac7002020-02-07 03:04:21 +0100288 Py_TYPE(m->m_self)->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290}
291
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000292static PyObject *
293meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyCFunctionObject *a, *b;
296 PyObject *res;
297 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if ((op != Py_EQ && op != Py_NE) ||
300 !PyCFunction_Check(self) ||
301 !PyCFunction_Check(other))
302 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500303 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 }
305 a = (PyCFunctionObject *)self;
306 b = (PyCFunctionObject *)other;
307 eq = a->m_self == b->m_self;
308 if (eq)
309 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
310 if (op == Py_EQ)
311 res = eq ? Py_True : Py_False;
312 else
313 res = eq ? Py_False : Py_True;
314 Py_INCREF(res);
315 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000316}
317
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000318static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000319meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000320{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000321 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300322 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 x ^= y;
325 if (x == -1)
326 x = -2;
327 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000328}
329
Tim Peters6d6c1a32001-08-02 04:15:00 +0000330
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyVarObject_HEAD_INIT(&PyType_Type, 0)
333 "builtin_function_or_method",
334 sizeof(PyCFunctionObject),
335 0,
336 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200337 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 0, /* tp_getattr */
339 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200340 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 (reprfunc)meth_repr, /* tp_repr */
342 0, /* tp_as_number */
343 0, /* tp_as_sequence */
344 0, /* tp_as_mapping */
345 (hashfunc)meth_hash, /* tp_hash */
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200346 cfunction_call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 0, /* tp_str */
348 PyObject_GenericGetAttr, /* tp_getattro */
349 0, /* tp_setattro */
350 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200351 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100352 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 0, /* tp_doc */
354 (traverseproc)meth_traverse, /* tp_traverse */
355 0, /* tp_clear */
356 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400357 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 0, /* tp_iter */
359 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800360 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 meth_members, /* tp_members */
362 meth_getsets, /* tp_getset */
363 0, /* tp_base */
364 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000366
Petr Viktorine1becf42020-05-07 15:39:59 +0200367PyTypeObject PyCMethod_Type = {
368 PyVarObject_HEAD_INIT(&PyType_Type, 0)
369 .tp_name = "builtin_method",
370 .tp_basicsize = sizeof(PyCMethodObject),
371 .tp_base = &PyCFunction_Type,
372};
373
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200374/* Vectorcall functions for each of the PyCFunction calling conventions,
375 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
376 * doesn't use vectorcall.
377 *
378 * First, common helpers
379 */
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200380
381static inline int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100382cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200383{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100384 assert(!_PyErr_Occurred(tstate));
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200385 assert(PyCFunction_Check(func));
386 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100387 PyObject *funcstr = _PyObject_FunctionStr(func);
388 if (funcstr != NULL) {
389 _PyErr_Format(tstate, PyExc_TypeError,
390 "%U takes no keyword arguments", funcstr);
391 Py_DECREF(funcstr);
392 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200393 return -1;
394 }
395 return 0;
396}
397
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100398typedef void (*funcptr)(void);
399
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200400static inline funcptr
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100401cfunction_enter_call(PyThreadState *tstate, PyObject *func)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200402{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100403 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200404 return NULL;
405 }
406 return (funcptr)PyCFunction_GET_FUNCTION(func);
407}
408
409/* Now the actual vectorcall functions */
410static PyObject *
411cfunction_vectorcall_FASTCALL(
412 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
413{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100414 PyThreadState *tstate = _PyThreadState_GET();
415 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200416 return NULL;
417 }
418 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
419 _PyCFunctionFast meth = (_PyCFunctionFast)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100420 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200421 if (meth == NULL) {
422 return NULL;
423 }
424 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100425 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200426 return result;
427}
428
429static PyObject *
430cfunction_vectorcall_FASTCALL_KEYWORDS(
431 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
432{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100433 PyThreadState *tstate = _PyThreadState_GET();
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200434 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
435 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100436 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200437 if (meth == NULL) {
438 return NULL;
439 }
440 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100441 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200442 return result;
443}
444
445static PyObject *
Petr Viktorine1becf42020-05-07 15:39:59 +0200446cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
447 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
448{
449 PyThreadState *tstate = _PyThreadState_GET();
450 PyTypeObject *cls = PyCFunction_GET_CLASS(func);
451 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
452 PyCMethod meth = (PyCMethod)cfunction_enter_call(tstate, func);
453 if (meth == NULL) {
454 return NULL;
455 }
456 PyObject *result = meth(PyCFunction_GET_SELF(func), cls, args, nargs, kwnames);
457 _Py_LeaveRecursiveCall(tstate);
458 return result;
459}
460
461static PyObject *
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200462cfunction_vectorcall_NOARGS(
463 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
464{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100465 PyThreadState *tstate = _PyThreadState_GET();
466 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200467 return NULL;
468 }
469 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
470 if (nargs != 0) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100471 PyObject *funcstr = _PyObject_FunctionStr(func);
472 if (funcstr != NULL) {
473 _PyErr_Format(tstate, PyExc_TypeError,
474 "%U takes no arguments (%zd given)", funcstr, nargs);
475 Py_DECREF(funcstr);
476 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200477 return NULL;
478 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100479 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200480 if (meth == NULL) {
481 return NULL;
482 }
483 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100484 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200485 return result;
486}
487
488static PyObject *
489cfunction_vectorcall_O(
490 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
491{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100492 PyThreadState *tstate = _PyThreadState_GET();
493 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200494 return NULL;
495 }
496 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
497 if (nargs != 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100498 PyObject *funcstr = _PyObject_FunctionStr(func);
499 if (funcstr != NULL) {
500 _PyErr_Format(tstate, PyExc_TypeError,
501 "%U takes exactly one argument (%zd given)", funcstr, nargs);
502 Py_DECREF(funcstr);
503 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200504 return NULL;
505 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100506 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200507 if (meth == NULL) {
508 return NULL;
509 }
510 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100511 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200512 return result;
513}
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200514
515
516static PyObject *
517cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
518{
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200519 assert(kwargs == NULL || PyDict_Check(kwargs));
520
Victor Stinner17269092019-11-05 01:22:12 +0100521 PyThreadState *tstate = _PyThreadState_GET();
522 assert(!_PyErr_Occurred(tstate));
523
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200524 int flags = PyCFunction_GET_FLAGS(func);
525 if (!(flags & METH_VARARGS)) {
526 /* If this is not a METH_VARARGS function, delegate to vectorcall */
527 return PyVectorcall_Call(func, args, kwargs);
528 }
529
530 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
531 * is NULL. This is intentional, since vectorcall would be slower. */
532 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
533 PyObject *self = PyCFunction_GET_SELF(func);
534
535 PyObject *result;
536 if (flags & METH_KEYWORDS) {
537 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
538 }
539 else {
540 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner17269092019-11-05 01:22:12 +0100541 _PyErr_Format(tstate, PyExc_TypeError,
542 "%.200s() takes no keyword arguments",
543 ((PyCFunctionObject*)func)->m_ml->ml_name);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200544 return NULL;
545 }
546 result = meth(self, args);
547 }
Victor Stinner17269092019-11-05 01:22:12 +0100548 return _Py_CheckFunctionResult(tstate, func, result, NULL);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200549}