blob: a5f0c5d3465def82da72985d9d90971ef1a89afc [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 Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +00008#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Andrew Svetlov4de29242012-12-26 23:08:54 +020010/* undefine macro trampoline to PyCFunction_NewEx */
11#undef PyCFunction_New
12
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020013/* Forward declarations */
14static PyObject * cfunction_vectorcall_FASTCALL(
15 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
16static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
17 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
18static PyObject * cfunction_vectorcall_NOARGS(
19 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
20static PyObject * cfunction_vectorcall_O(
21 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +020022static PyObject * cfunction_call(
23 PyObject *func, PyObject *args, PyObject *kwargs);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020024
25
Benjamin Petersone5024512018-09-12 12:06:42 -070026PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020027PyCFunction_New(PyMethodDef *ml, PyObject *self)
28{
29 return PyCFunction_NewEx(ml, self, NULL);
30}
31
32PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000033PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034{
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020035 /* Figure out correct vectorcall function to use */
36 vectorcallfunc vectorcall;
37 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS))
38 {
39 case METH_VARARGS:
40 case METH_VARARGS | METH_KEYWORDS:
41 /* For METH_VARARGS functions, it's more efficient to use tp_call
42 * instead of vectorcall. */
43 vectorcall = NULL;
44 break;
45 case METH_FASTCALL:
46 vectorcall = cfunction_vectorcall_FASTCALL;
47 break;
48 case METH_FASTCALL | METH_KEYWORDS:
49 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
50 break;
51 case METH_NOARGS:
52 vectorcall = cfunction_vectorcall_NOARGS;
53 break;
54 case METH_O:
55 vectorcall = cfunction_vectorcall_O;
56 break;
57 default:
58 PyErr_SetString(PyExc_SystemError, "bad call flags");
59 return NULL;
60 }
61
Inada Naoki3e54b572019-07-26 15:05:50 +090062 PyCFunctionObject *op =
63 PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
64 if (op == NULL) {
65 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040067 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 op->m_ml = ml;
69 Py_XINCREF(self);
70 op->m_self = self;
71 Py_XINCREF(module);
72 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020073 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 _PyObject_GC_TRACK(op);
75 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076}
77
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000079PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (!PyCFunction_Check(op)) {
82 PyErr_BadInternalCall();
83 return NULL;
84 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010085 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086}
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000089PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (!PyCFunction_Check(op)) {
92 PyErr_BadInternalCall();
93 return NULL;
94 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010095 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096}
97
Guido van Rossumc0602291991-12-16 13:07:24 +000098int
Fred Drakeee238b92000-07-09 06:03:25 +000099PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (!PyCFunction_Check(op)) {
102 PyErr_BadInternalCall();
103 return -1;
104 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100105 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000106}
107
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108/* Methods (the standard built-in methods, that is) */
109
110static void
Fred Drakeee238b92000-07-09 06:03:25 +0000111meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400114 if (m->m_weakreflist != NULL) {
115 PyObject_ClearWeakRefs((PyObject*) m);
116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 Py_XDECREF(m->m_self);
118 Py_XDECREF(m->m_module);
Inada Naoki3e54b572019-07-26 15:05:50 +0900119 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120}
121
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800122static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530123meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800124{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800125 _Py_IDENTIFIER(getattr);
126
127 if (m->m_self == NULL || PyModule_Check(m->m_self))
128 return PyUnicode_FromString(m->m_ml->ml_name);
129
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200130 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
131 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800132}
133
134static PyMethodDef meth_methods[] = {
135 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
136 {NULL, NULL}
137};
138
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800139static PyObject *
140meth_get__text_signature__(PyCFunctionObject *m, void *closure)
141{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800142 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800143}
144
Tim Peters6d6c1a32001-08-02 04:15:00 +0000145static PyObject *
146meth_get__doc__(PyCFunctionObject *m, void *closure)
147{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800148 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000149}
150
151static PyObject *
152meth_get__name__(PyCFunctionObject *m, void *closure)
153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000155}
156
Antoine Pitrou5b629422011-12-23 12:40:16 +0100157static PyObject *
158meth_get__qualname__(PyCFunctionObject *m, void *closure)
159{
160 /* If __self__ is a module or NULL, return m.__name__
161 (e.g. len.__qualname__ == 'len')
162
163 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
164 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
165
166 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
167 (e.g. [].append.__qualname__ == 'list.append') */
168 PyObject *type, *type_qualname, *res;
169 _Py_IDENTIFIER(__qualname__);
170
171 if (m->m_self == NULL || PyModule_Check(m->m_self))
172 return PyUnicode_FromString(m->m_ml->ml_name);
173
174 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
175
176 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
177 if (type_qualname == NULL)
178 return NULL;
179
180 if (!PyUnicode_Check(type_qualname)) {
181 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
182 "__qualname__ is not a unicode object");
183 Py_XDECREF(type_qualname);
184 return NULL;
185 }
186
187 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
188 Py_DECREF(type_qualname);
189 return res;
190}
191
Neil Schemenauer10c66922001-07-12 13:27:35 +0000192static int
193meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 Py_VISIT(m->m_self);
196 Py_VISIT(m->m_module);
197 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000198}
199
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000201meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000204
Antoine Pitrou5b629422011-12-23 12:40:16 +0100205 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (self == NULL)
207 self = Py_None;
208 Py_INCREF(self);
209 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000210}
211
Guido van Rossum32d34c82001-09-20 21:45:26 +0000212static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
214 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100215 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800217 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000219};
220
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000221#define OFF(x) offsetof(PyCFunctionObject, x)
222
223static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
225 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000226};
227
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000229meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (m->m_self == NULL || PyModule_Check(m->m_self))
232 return PyUnicode_FromFormat("<built-in function %s>",
233 m->m_ml->ml_name);
234 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
235 m->m_ml->ml_name,
236 m->m_self->ob_type->tp_name,
237 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238}
239
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000240static PyObject *
241meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 PyCFunctionObject *a, *b;
244 PyObject *res;
245 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if ((op != Py_EQ && op != Py_NE) ||
248 !PyCFunction_Check(self) ||
249 !PyCFunction_Check(other))
250 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500251 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 }
253 a = (PyCFunctionObject *)self;
254 b = (PyCFunctionObject *)other;
255 eq = a->m_self == b->m_self;
256 if (eq)
257 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
258 if (op == Py_EQ)
259 res = eq ? Py_True : Py_False;
260 else
261 res = eq ? Py_False : Py_True;
262 Py_INCREF(res);
263 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264}
265
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000266static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000267meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000268{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000269 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300270 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 x ^= y;
273 if (x == -1)
274 x = -2;
275 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000276}
277
Tim Peters6d6c1a32001-08-02 04:15:00 +0000278
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PyVarObject_HEAD_INIT(&PyType_Type, 0)
281 "builtin_function_or_method",
282 sizeof(PyCFunctionObject),
283 0,
284 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200285 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 0, /* tp_getattr */
287 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200288 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 (reprfunc)meth_repr, /* tp_repr */
290 0, /* tp_as_number */
291 0, /* tp_as_sequence */
292 0, /* tp_as_mapping */
293 (hashfunc)meth_hash, /* tp_hash */
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200294 cfunction_call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 0, /* tp_str */
296 PyObject_GenericGetAttr, /* tp_getattro */
297 0, /* tp_setattro */
298 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200299 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
300 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 0, /* tp_doc */
302 (traverseproc)meth_traverse, /* tp_traverse */
303 0, /* tp_clear */
304 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400305 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 0, /* tp_iter */
307 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800308 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 meth_members, /* tp_members */
310 meth_getsets, /* tp_getset */
311 0, /* tp_base */
312 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000314
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000315/* Clear out the free list */
316
Christian Heimesa156e092008-02-16 07:38:31 +0000317int
318PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000319{
Inada Naoki3e54b572019-07-26 15:05:50 +0900320 return 0;
Christian Heimesa156e092008-02-16 07:38:31 +0000321}
322
323void
Victor Stinnerbed48172019-08-27 00:12:32 +0200324_PyCFunction_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000327}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000328
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200329
330/* Vectorcall functions for each of the PyCFunction calling conventions,
331 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
332 * doesn't use vectorcall.
333 *
334 * First, common helpers
335 */
336static const char *
337get_name(PyObject *func)
338{
339 assert(PyCFunction_Check(func));
340 PyMethodDef *method = ((PyCFunctionObject *)func)->m_ml;
341 return method->ml_name;
342}
343
344typedef void (*funcptr)(void);
345
346static inline int
347cfunction_check_kwargs(PyObject *func, PyObject *kwnames)
348{
349 assert(!PyErr_Occurred());
350 assert(PyCFunction_Check(func));
351 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
352 PyErr_Format(PyExc_TypeError,
353 "%.200s() takes no keyword arguments", get_name(func));
354 return -1;
355 }
356 return 0;
357}
358
359static inline funcptr
360cfunction_enter_call(PyObject *func)
361{
362 if (Py_EnterRecursiveCall(" while calling a Python object")) {
363 return NULL;
364 }
365 return (funcptr)PyCFunction_GET_FUNCTION(func);
366}
367
368/* Now the actual vectorcall functions */
369static PyObject *
370cfunction_vectorcall_FASTCALL(
371 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
372{
373 if (cfunction_check_kwargs(func, kwnames)) {
374 return NULL;
375 }
376 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
377 _PyCFunctionFast meth = (_PyCFunctionFast)
378 cfunction_enter_call(func);
379 if (meth == NULL) {
380 return NULL;
381 }
382 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
383 Py_LeaveRecursiveCall();
384 return result;
385}
386
387static PyObject *
388cfunction_vectorcall_FASTCALL_KEYWORDS(
389 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
390{
391 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
392 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
393 cfunction_enter_call(func);
394 if (meth == NULL) {
395 return NULL;
396 }
397 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
398 Py_LeaveRecursiveCall();
399 return result;
400}
401
402static PyObject *
403cfunction_vectorcall_NOARGS(
404 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
405{
406 if (cfunction_check_kwargs(func, kwnames)) {
407 return NULL;
408 }
409 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
410 if (nargs != 0) {
411 PyErr_Format(PyExc_TypeError,
412 "%.200s() takes no arguments (%zd given)", get_name(func), nargs);
413 return NULL;
414 }
415 PyCFunction meth = (PyCFunction)cfunction_enter_call(func);
416 if (meth == NULL) {
417 return NULL;
418 }
419 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
420 Py_LeaveRecursiveCall();
421 return result;
422}
423
424static PyObject *
425cfunction_vectorcall_O(
426 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
427{
428 if (cfunction_check_kwargs(func, kwnames)) {
429 return NULL;
430 }
431 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
432 if (nargs != 1) {
433 PyErr_Format(PyExc_TypeError,
434 "%.200s() takes exactly one argument (%zd given)",
435 get_name(func), nargs);
436 return NULL;
437 }
438 PyCFunction meth = (PyCFunction)cfunction_enter_call(func);
439 if (meth == NULL) {
440 return NULL;
441 }
442 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
443 Py_LeaveRecursiveCall();
444 return result;
445}
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200446
447
448static PyObject *
449cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
450{
451 assert(!PyErr_Occurred());
452 assert(kwargs == NULL || PyDict_Check(kwargs));
453
454 int flags = PyCFunction_GET_FLAGS(func);
455 if (!(flags & METH_VARARGS)) {
456 /* If this is not a METH_VARARGS function, delegate to vectorcall */
457 return PyVectorcall_Call(func, args, kwargs);
458 }
459
460 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
461 * is NULL. This is intentional, since vectorcall would be slower. */
462 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
463 PyObject *self = PyCFunction_GET_SELF(func);
464
465 PyObject *result;
466 if (flags & METH_KEYWORDS) {
467 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
468 }
469 else {
470 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
471 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
472 ((PyCFunctionObject*)func)->m_ml->ml_name);
473 return NULL;
474 }
475 result = meth(self, args);
476 }
477 return _Py_CheckFunctionResult(func, result, NULL);
478}