blob: 8f752c610ce4b97c2afdd62a8e731e19b88330cf [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:
59 PyErr_SetString(PyExc_SystemError, "bad call flags");
60 return NULL;
61 }
62
Inada Naoki3e54b572019-07-26 15:05:50 +090063 PyCFunctionObject *op =
64 PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
65 if (op == NULL) {
66 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040068 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 op->m_ml = ml;
70 Py_XINCREF(self);
71 op->m_self = self;
72 Py_XINCREF(module);
73 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020074 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 _PyObject_GC_TRACK(op);
76 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077}
78
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000080PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (!PyCFunction_Check(op)) {
83 PyErr_BadInternalCall();
84 return NULL;
85 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010086 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087}
88
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000090PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (!PyCFunction_Check(op)) {
93 PyErr_BadInternalCall();
94 return NULL;
95 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010096 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097}
98
Guido van Rossumc0602291991-12-16 13:07:24 +000099int
Fred Drakeee238b92000-07-09 06:03:25 +0000100PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +0000101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 if (!PyCFunction_Check(op)) {
103 PyErr_BadInternalCall();
104 return -1;
105 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100106 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000107}
108
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109/* Methods (the standard built-in methods, that is) */
110
111static void
Fred Drakeee238b92000-07-09 06:03:25 +0000112meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400115 if (m->m_weakreflist != NULL) {
116 PyObject_ClearWeakRefs((PyObject*) m);
117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 Py_XDECREF(m->m_self);
119 Py_XDECREF(m->m_module);
Inada Naoki3e54b572019-07-26 15:05:50 +0900120 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800123static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530124meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800125{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800126 _Py_IDENTIFIER(getattr);
127
128 if (m->m_self == NULL || PyModule_Check(m->m_self))
129 return PyUnicode_FromString(m->m_ml->ml_name);
130
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200131 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
132 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800133}
134
135static PyMethodDef meth_methods[] = {
136 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
137 {NULL, NULL}
138};
139
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800140static PyObject *
141meth_get__text_signature__(PyCFunctionObject *m, void *closure)
142{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800143 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800144}
145
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146static PyObject *
147meth_get__doc__(PyCFunctionObject *m, void *closure)
148{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800149 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000150}
151
152static PyObject *
153meth_get__name__(PyCFunctionObject *m, void *closure)
154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000156}
157
Antoine Pitrou5b629422011-12-23 12:40:16 +0100158static PyObject *
159meth_get__qualname__(PyCFunctionObject *m, void *closure)
160{
161 /* If __self__ is a module or NULL, return m.__name__
162 (e.g. len.__qualname__ == 'len')
163
164 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
165 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
166
167 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
168 (e.g. [].append.__qualname__ == 'list.append') */
169 PyObject *type, *type_qualname, *res;
170 _Py_IDENTIFIER(__qualname__);
171
172 if (m->m_self == NULL || PyModule_Check(m->m_self))
173 return PyUnicode_FromString(m->m_ml->ml_name);
174
175 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
176
177 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
178 if (type_qualname == NULL)
179 return NULL;
180
181 if (!PyUnicode_Check(type_qualname)) {
182 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
183 "__qualname__ is not a unicode object");
184 Py_XDECREF(type_qualname);
185 return NULL;
186 }
187
188 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
189 Py_DECREF(type_qualname);
190 return res;
191}
192
Neil Schemenauer10c66922001-07-12 13:27:35 +0000193static int
194meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 Py_VISIT(m->m_self);
197 Py_VISIT(m->m_module);
198 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000199}
200
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000202meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000205
Antoine Pitrou5b629422011-12-23 12:40:16 +0100206 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (self == NULL)
208 self = Py_None;
209 Py_INCREF(self);
210 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000211}
212
Guido van Rossum32d34c82001-09-20 21:45:26 +0000213static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
215 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100216 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800218 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000220};
221
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000222#define OFF(x) offsetof(PyCFunctionObject, x)
223
224static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
226 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000227};
228
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000230meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (m->m_self == NULL || PyModule_Check(m->m_self))
233 return PyUnicode_FromFormat("<built-in function %s>",
234 m->m_ml->ml_name);
235 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
236 m->m_ml->ml_name,
237 m->m_self->ob_type->tp_name,
238 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239}
240
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000241static PyObject *
242meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyCFunctionObject *a, *b;
245 PyObject *res;
246 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if ((op != Py_EQ && op != Py_NE) ||
249 !PyCFunction_Check(self) ||
250 !PyCFunction_Check(other))
251 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500252 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 }
254 a = (PyCFunctionObject *)self;
255 b = (PyCFunctionObject *)other;
256 eq = a->m_self == b->m_self;
257 if (eq)
258 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
259 if (op == Py_EQ)
260 res = eq ? Py_True : Py_False;
261 else
262 res = eq ? Py_False : Py_True;
263 Py_INCREF(res);
264 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000265}
266
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000267static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000268meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000270 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300271 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 x ^= y;
274 if (x == -1)
275 x = -2;
276 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000277}
278
Tim Peters6d6c1a32001-08-02 04:15:00 +0000279
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 PyVarObject_HEAD_INIT(&PyType_Type, 0)
282 "builtin_function_or_method",
283 sizeof(PyCFunctionObject),
284 0,
285 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200286 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 0, /* tp_getattr */
288 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200289 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 (reprfunc)meth_repr, /* tp_repr */
291 0, /* tp_as_number */
292 0, /* tp_as_sequence */
293 0, /* tp_as_mapping */
294 (hashfunc)meth_hash, /* tp_hash */
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200295 cfunction_call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 0, /* tp_str */
297 PyObject_GenericGetAttr, /* tp_getattro */
298 0, /* tp_setattro */
299 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200300 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
301 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 0, /* tp_doc */
303 (traverseproc)meth_traverse, /* tp_traverse */
304 0, /* tp_clear */
305 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400306 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 0, /* tp_iter */
308 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800309 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 meth_members, /* tp_members */
311 meth_getsets, /* tp_getset */
312 0, /* tp_base */
313 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000316/* Clear out the free list */
317
Christian Heimesa156e092008-02-16 07:38:31 +0000318int
319PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000320{
Inada Naoki3e54b572019-07-26 15:05:50 +0900321 return 0;
Christian Heimesa156e092008-02-16 07:38:31 +0000322}
323
324void
Victor Stinnerbed48172019-08-27 00:12:32 +0200325_PyCFunction_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000328}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000329
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200330
331/* Vectorcall functions for each of the PyCFunction calling conventions,
332 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
333 * doesn't use vectorcall.
334 *
335 * First, common helpers
336 */
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200337
338static inline int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100339cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200340{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100341 assert(!_PyErr_Occurred(tstate));
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200342 assert(PyCFunction_Check(func));
343 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100344 PyObject *funcstr = _PyObject_FunctionStr(func);
345 if (funcstr != NULL) {
346 _PyErr_Format(tstate, PyExc_TypeError,
347 "%U takes no keyword arguments", funcstr);
348 Py_DECREF(funcstr);
349 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200350 return -1;
351 }
352 return 0;
353}
354
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100355typedef void (*funcptr)(void);
356
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200357static inline funcptr
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100358cfunction_enter_call(PyThreadState *tstate, PyObject *func)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200359{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100360 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200361 return NULL;
362 }
363 return (funcptr)PyCFunction_GET_FUNCTION(func);
364}
365
366/* Now the actual vectorcall functions */
367static PyObject *
368cfunction_vectorcall_FASTCALL(
369 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
370{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100371 PyThreadState *tstate = _PyThreadState_GET();
372 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200373 return NULL;
374 }
375 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
376 _PyCFunctionFast meth = (_PyCFunctionFast)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100377 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200378 if (meth == NULL) {
379 return NULL;
380 }
381 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100382 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200383 return result;
384}
385
386static PyObject *
387cfunction_vectorcall_FASTCALL_KEYWORDS(
388 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
389{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100390 PyThreadState *tstate = _PyThreadState_GET();
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200391 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
392 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100393 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200394 if (meth == NULL) {
395 return NULL;
396 }
397 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100398 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200399 return result;
400}
401
402static PyObject *
403cfunction_vectorcall_NOARGS(
404 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
405{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100406 PyThreadState *tstate = _PyThreadState_GET();
407 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200408 return NULL;
409 }
410 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
411 if (nargs != 0) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100412 PyObject *funcstr = _PyObject_FunctionStr(func);
413 if (funcstr != NULL) {
414 _PyErr_Format(tstate, PyExc_TypeError,
415 "%U takes no arguments (%zd given)", funcstr, nargs);
416 Py_DECREF(funcstr);
417 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200418 return NULL;
419 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100420 PyCFunction meth = (PyCFunction)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), NULL);
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_O(
431 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
432{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100433 PyThreadState *tstate = _PyThreadState_GET();
434 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200435 return NULL;
436 }
437 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
438 if (nargs != 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100439 PyObject *funcstr = _PyObject_FunctionStr(func);
440 if (funcstr != NULL) {
441 _PyErr_Format(tstate, PyExc_TypeError,
442 "%U takes exactly one argument (%zd given)", funcstr, nargs);
443 Py_DECREF(funcstr);
444 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200445 return NULL;
446 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100447 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200448 if (meth == NULL) {
449 return NULL;
450 }
451 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100452 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200453 return result;
454}
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200455
456
457static PyObject *
458cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
459{
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200460 assert(kwargs == NULL || PyDict_Check(kwargs));
461
Victor Stinner17269092019-11-05 01:22:12 +0100462 PyThreadState *tstate = _PyThreadState_GET();
463 assert(!_PyErr_Occurred(tstate));
464
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200465 int flags = PyCFunction_GET_FLAGS(func);
466 if (!(flags & METH_VARARGS)) {
467 /* If this is not a METH_VARARGS function, delegate to vectorcall */
468 return PyVectorcall_Call(func, args, kwargs);
469 }
470
471 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
472 * is NULL. This is intentional, since vectorcall would be slower. */
473 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
474 PyObject *self = PyCFunction_GET_SELF(func);
475
476 PyObject *result;
477 if (flags & METH_KEYWORDS) {
478 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
479 }
480 else {
481 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner17269092019-11-05 01:22:12 +0100482 _PyErr_Format(tstate, PyExc_TypeError,
483 "%.200s() takes no keyword arguments",
484 ((PyCFunctionObject*)func)->m_ml->ml_name);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200485 return NULL;
486 }
487 result = meth(self, args);
488 }
Victor Stinner17269092019-11-05 01:22:12 +0100489 return _Py_CheckFunctionResult(tstate, func, result, NULL);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200490}