blob: 16abded385466403dd2788331df09687caf12539 [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 Stinnerbe434dc2019-11-05 00:51:22 +01006#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pymem.h"
8#include "pycore_pystate.h"
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +00009#include "structmember.h"
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
13
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020014/* Forward declarations */
15static PyObject * cfunction_vectorcall_FASTCALL(
16 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
17static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
18 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
19static PyObject * cfunction_vectorcall_NOARGS(
20 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
21static PyObject * cfunction_vectorcall_O(
22 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +020023static PyObject * cfunction_call(
24 PyObject *func, PyObject *args, PyObject *kwargs);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020025
26
Benjamin Petersone5024512018-09-12 12:06:42 -070027PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020028PyCFunction_New(PyMethodDef *ml, PyObject *self)
29{
30 return PyCFunction_NewEx(ml, self, NULL);
31}
32
33PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000034PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035{
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020036 /* Figure out correct vectorcall function to use */
37 vectorcallfunc vectorcall;
38 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS))
39 {
40 case METH_VARARGS:
41 case METH_VARARGS | METH_KEYWORDS:
42 /* For METH_VARARGS functions, it's more efficient to use tp_call
43 * instead of vectorcall. */
44 vectorcall = NULL;
45 break;
46 case METH_FASTCALL:
47 vectorcall = cfunction_vectorcall_FASTCALL;
48 break;
49 case METH_FASTCALL | METH_KEYWORDS:
50 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
51 break;
52 case METH_NOARGS:
53 vectorcall = cfunction_vectorcall_NOARGS;
54 break;
55 case METH_O:
56 vectorcall = cfunction_vectorcall_O;
57 break;
58 default:
Victor Stinnerc7d2d692020-03-12 08:38:11 +010059 PyErr_Format(PyExc_SystemError,
60 "%s() method: bad call flags", ml->ml_name);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020061 return NULL;
62 }
63
Inada Naoki3e54b572019-07-26 15:05:50 +090064 PyCFunctionObject *op =
65 PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
66 if (op == NULL) {
67 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040069 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 op->m_ml = ml;
71 Py_XINCREF(self);
72 op->m_self = self;
73 Py_XINCREF(module);
74 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020075 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 _PyObject_GC_TRACK(op);
77 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078}
79
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000081PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if (!PyCFunction_Check(op)) {
84 PyErr_BadInternalCall();
85 return NULL;
86 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010087 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088}
89
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000091PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (!PyCFunction_Check(op)) {
94 PyErr_BadInternalCall();
95 return NULL;
96 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010097 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098}
99
Guido van Rossumc0602291991-12-16 13:07:24 +0000100int
Fred Drakeee238b92000-07-09 06:03:25 +0000101PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (!PyCFunction_Check(op)) {
104 PyErr_BadInternalCall();
105 return -1;
106 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100107 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000108}
109
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110/* Methods (the standard built-in methods, that is) */
111
112static void
Fred Drakeee238b92000-07-09 06:03:25 +0000113meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400116 if (m->m_weakreflist != NULL) {
117 PyObject_ClearWeakRefs((PyObject*) m);
118 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 Py_XDECREF(m->m_self);
120 Py_XDECREF(m->m_module);
Inada Naoki3e54b572019-07-26 15:05:50 +0900121 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122}
123
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800124static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530125meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800126{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800127 _Py_IDENTIFIER(getattr);
128
129 if (m->m_self == NULL || PyModule_Check(m->m_self))
130 return PyUnicode_FromString(m->m_ml->ml_name);
131
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200132 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
133 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800134}
135
136static PyMethodDef meth_methods[] = {
137 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
138 {NULL, NULL}
139};
140
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800141static PyObject *
142meth_get__text_signature__(PyCFunctionObject *m, void *closure)
143{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800144 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800145}
146
Tim Peters6d6c1a32001-08-02 04:15:00 +0000147static PyObject *
148meth_get__doc__(PyCFunctionObject *m, void *closure)
149{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800150 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000151}
152
153static PyObject *
154meth_get__name__(PyCFunctionObject *m, void *closure)
155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000157}
158
Antoine Pitrou5b629422011-12-23 12:40:16 +0100159static PyObject *
160meth_get__qualname__(PyCFunctionObject *m, void *closure)
161{
162 /* If __self__ is a module or NULL, return m.__name__
163 (e.g. len.__qualname__ == 'len')
164
165 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
166 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
167
168 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
169 (e.g. [].append.__qualname__ == 'list.append') */
170 PyObject *type, *type_qualname, *res;
171 _Py_IDENTIFIER(__qualname__);
172
173 if (m->m_self == NULL || PyModule_Check(m->m_self))
174 return PyUnicode_FromString(m->m_ml->ml_name);
175
176 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
177
178 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
179 if (type_qualname == NULL)
180 return NULL;
181
182 if (!PyUnicode_Check(type_qualname)) {
183 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
184 "__qualname__ is not a unicode object");
185 Py_XDECREF(type_qualname);
186 return NULL;
187 }
188
189 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
190 Py_DECREF(type_qualname);
191 return res;
192}
193
Neil Schemenauer10c66922001-07-12 13:27:35 +0000194static int
195meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 Py_VISIT(m->m_self);
198 Py_VISIT(m->m_module);
199 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000200}
201
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000203meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000206
Antoine Pitrou5b629422011-12-23 12:40:16 +0100207 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (self == NULL)
209 self = Py_None;
210 Py_INCREF(self);
211 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000212}
213
Guido van Rossum32d34c82001-09-20 21:45:26 +0000214static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
216 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100217 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800219 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000221};
222
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000223#define OFF(x) offsetof(PyCFunctionObject, x)
224
225static PyMemberDef meth_members[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100226 {"__module__", T_OBJECT, OFF(m_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000228};
229
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000231meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (m->m_self == NULL || PyModule_Check(m->m_self))
234 return PyUnicode_FromFormat("<built-in function %s>",
235 m->m_ml->ml_name);
236 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
237 m->m_ml->ml_name,
Victor Stinner58ac7002020-02-07 03:04:21 +0100238 Py_TYPE(m->m_self)->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240}
241
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000242static PyObject *
243meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyCFunctionObject *a, *b;
246 PyObject *res;
247 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if ((op != Py_EQ && op != Py_NE) ||
250 !PyCFunction_Check(self) ||
251 !PyCFunction_Check(other))
252 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500253 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 }
255 a = (PyCFunctionObject *)self;
256 b = (PyCFunctionObject *)other;
257 eq = a->m_self == b->m_self;
258 if (eq)
259 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
260 if (op == Py_EQ)
261 res = eq ? Py_True : Py_False;
262 else
263 res = eq ? Py_False : Py_True;
264 Py_INCREF(res);
265 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000266}
267
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000268static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000269meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000271 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300272 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 x ^= y;
275 if (x == -1)
276 x = -2;
277 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000278}
279
Tim Peters6d6c1a32001-08-02 04:15:00 +0000280
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 PyVarObject_HEAD_INIT(&PyType_Type, 0)
283 "builtin_function_or_method",
284 sizeof(PyCFunctionObject),
285 0,
286 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200287 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 0, /* tp_getattr */
289 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200290 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 (reprfunc)meth_repr, /* tp_repr */
292 0, /* tp_as_number */
293 0, /* tp_as_sequence */
294 0, /* tp_as_mapping */
295 (hashfunc)meth_hash, /* tp_hash */
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200296 cfunction_call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 0, /* tp_str */
298 PyObject_GenericGetAttr, /* tp_getattro */
299 0, /* tp_setattro */
300 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200301 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100302 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 0, /* tp_doc */
304 (traverseproc)meth_traverse, /* tp_traverse */
305 0, /* tp_clear */
306 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400307 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 0, /* tp_iter */
309 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800310 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 meth_members, /* tp_members */
312 meth_getsets, /* tp_getset */
313 0, /* tp_base */
314 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000316
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200317/* Vectorcall functions for each of the PyCFunction calling conventions,
318 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
319 * doesn't use vectorcall.
320 *
321 * First, common helpers
322 */
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200323
324static inline int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100325cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200326{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100327 assert(!_PyErr_Occurred(tstate));
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200328 assert(PyCFunction_Check(func));
329 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100330 PyObject *funcstr = _PyObject_FunctionStr(func);
331 if (funcstr != NULL) {
332 _PyErr_Format(tstate, PyExc_TypeError,
333 "%U takes no keyword arguments", funcstr);
334 Py_DECREF(funcstr);
335 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200336 return -1;
337 }
338 return 0;
339}
340
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100341typedef void (*funcptr)(void);
342
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200343static inline funcptr
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100344cfunction_enter_call(PyThreadState *tstate, PyObject *func)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200345{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100346 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200347 return NULL;
348 }
349 return (funcptr)PyCFunction_GET_FUNCTION(func);
350}
351
352/* Now the actual vectorcall functions */
353static PyObject *
354cfunction_vectorcall_FASTCALL(
355 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
356{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100357 PyThreadState *tstate = _PyThreadState_GET();
358 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200359 return NULL;
360 }
361 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
362 _PyCFunctionFast meth = (_PyCFunctionFast)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100363 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200364 if (meth == NULL) {
365 return NULL;
366 }
367 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100368 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200369 return result;
370}
371
372static PyObject *
373cfunction_vectorcall_FASTCALL_KEYWORDS(
374 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
375{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100376 PyThreadState *tstate = _PyThreadState_GET();
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200377 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
378 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100379 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200380 if (meth == NULL) {
381 return NULL;
382 }
383 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100384 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200385 return result;
386}
387
388static PyObject *
389cfunction_vectorcall_NOARGS(
390 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
391{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100392 PyThreadState *tstate = _PyThreadState_GET();
393 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200394 return NULL;
395 }
396 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
397 if (nargs != 0) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100398 PyObject *funcstr = _PyObject_FunctionStr(func);
399 if (funcstr != NULL) {
400 _PyErr_Format(tstate, PyExc_TypeError,
401 "%U takes no arguments (%zd given)", funcstr, nargs);
402 Py_DECREF(funcstr);
403 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200404 return NULL;
405 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100406 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200407 if (meth == NULL) {
408 return NULL;
409 }
410 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100411 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200412 return result;
413}
414
415static PyObject *
416cfunction_vectorcall_O(
417 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
418{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100419 PyThreadState *tstate = _PyThreadState_GET();
420 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200421 return NULL;
422 }
423 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
424 if (nargs != 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100425 PyObject *funcstr = _PyObject_FunctionStr(func);
426 if (funcstr != NULL) {
427 _PyErr_Format(tstate, PyExc_TypeError,
428 "%U takes exactly one argument (%zd given)", funcstr, nargs);
429 Py_DECREF(funcstr);
430 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200431 return NULL;
432 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100433 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200434 if (meth == NULL) {
435 return NULL;
436 }
437 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100438 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200439 return result;
440}
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200441
442
443static PyObject *
444cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
445{
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200446 assert(kwargs == NULL || PyDict_Check(kwargs));
447
Victor Stinner17269092019-11-05 01:22:12 +0100448 PyThreadState *tstate = _PyThreadState_GET();
449 assert(!_PyErr_Occurred(tstate));
450
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200451 int flags = PyCFunction_GET_FLAGS(func);
452 if (!(flags & METH_VARARGS)) {
453 /* If this is not a METH_VARARGS function, delegate to vectorcall */
454 return PyVectorcall_Call(func, args, kwargs);
455 }
456
457 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
458 * is NULL. This is intentional, since vectorcall would be slower. */
459 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
460 PyObject *self = PyCFunction_GET_SELF(func);
461
462 PyObject *result;
463 if (flags & METH_KEYWORDS) {
464 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
465 }
466 else {
467 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner17269092019-11-05 01:22:12 +0100468 _PyErr_Format(tstate, PyExc_TypeError,
469 "%.200s() takes no keyword arguments",
470 ((PyCFunctionObject*)func)->m_ml->ml_name);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200471 return NULL;
472 }
473 result = meth(self, args);
474 }
Victor Stinner17269092019-11-05 01:22:12 +0100475 return _Py_CheckFunctionResult(tstate, func, result, NULL);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200476}