blob: 44ae00f7e00236ef9cd2d4e2d3c138e1ee91d107 [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 Stinner224481a2020-03-13 10:19:38 +01005#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 Stinner621cebe2018-11-12 16:53:38 +01008#include "pycore_pymem.h"
9#include "pycore_pystate.h"
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000010#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Andrew Svetlov4de29242012-12-26 23:08:54 +020012/* undefine macro trampoline to PyCFunction_NewEx */
13#undef PyCFunction_New
14
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020015/* Forward declarations */
16static PyObject * cfunction_vectorcall_FASTCALL(
17 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
18static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
19 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
20static PyObject * cfunction_vectorcall_NOARGS(
21 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
22static PyObject * cfunction_vectorcall_O(
23 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +020024static PyObject * cfunction_call(
25 PyObject *func, PyObject *args, PyObject *kwargs);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020026
27
Benjamin Petersone5024512018-09-12 12:06:42 -070028PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020029PyCFunction_New(PyMethodDef *ml, PyObject *self)
30{
31 return PyCFunction_NewEx(ml, self, NULL);
32}
33
34PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000035PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036{
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020037 /* Figure out correct vectorcall function to use */
38 vectorcallfunc vectorcall;
39 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS))
40 {
41 case METH_VARARGS:
42 case METH_VARARGS | METH_KEYWORDS:
43 /* For METH_VARARGS functions, it's more efficient to use tp_call
44 * instead of vectorcall. */
45 vectorcall = NULL;
46 break;
47 case METH_FASTCALL:
48 vectorcall = cfunction_vectorcall_FASTCALL;
49 break;
50 case METH_FASTCALL | METH_KEYWORDS:
51 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
52 break;
53 case METH_NOARGS:
54 vectorcall = cfunction_vectorcall_NOARGS;
55 break;
56 case METH_O:
57 vectorcall = cfunction_vectorcall_O;
58 break;
59 default:
Victor Stinnerc7d2d692020-03-12 08:38:11 +010060 PyErr_Format(PyExc_SystemError,
61 "%s() method: bad call flags", ml->ml_name);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020062 return NULL;
63 }
64
Inada Naoki3e54b572019-07-26 15:05:50 +090065 PyCFunctionObject *op =
66 PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
67 if (op == NULL) {
68 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040070 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 op->m_ml = ml;
72 Py_XINCREF(self);
73 op->m_self = self;
74 Py_XINCREF(module);
75 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020076 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 _PyObject_GC_TRACK(op);
78 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079}
80
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000082PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (!PyCFunction_Check(op)) {
85 PyErr_BadInternalCall();
86 return NULL;
87 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010088 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000092PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (!PyCFunction_Check(op)) {
95 PyErr_BadInternalCall();
96 return NULL;
97 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010098 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
Guido van Rossumc0602291991-12-16 13:07:24 +0000101int
Fred Drakeee238b92000-07-09 06:03:25 +0000102PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (!PyCFunction_Check(op)) {
105 PyErr_BadInternalCall();
106 return -1;
107 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100108 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000109}
110
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111/* Methods (the standard built-in methods, that is) */
112
113static void
Fred Drakeee238b92000-07-09 06:03:25 +0000114meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400117 if (m->m_weakreflist != NULL) {
118 PyObject_ClearWeakRefs((PyObject*) m);
119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 Py_XDECREF(m->m_self);
121 Py_XDECREF(m->m_module);
Inada Naoki3e54b572019-07-26 15:05:50 +0900122 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}
124
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800125static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530126meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800127{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800128 _Py_IDENTIFIER(getattr);
129
130 if (m->m_self == NULL || PyModule_Check(m->m_self))
131 return PyUnicode_FromString(m->m_ml->ml_name);
132
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200133 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
134 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800135}
136
137static PyMethodDef meth_methods[] = {
138 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
139 {NULL, NULL}
140};
141
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800142static PyObject *
143meth_get__text_signature__(PyCFunctionObject *m, void *closure)
144{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800145 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800146}
147
Tim Peters6d6c1a32001-08-02 04:15:00 +0000148static PyObject *
149meth_get__doc__(PyCFunctionObject *m, void *closure)
150{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800151 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152}
153
154static PyObject *
155meth_get__name__(PyCFunctionObject *m, void *closure)
156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000158}
159
Antoine Pitrou5b629422011-12-23 12:40:16 +0100160static PyObject *
161meth_get__qualname__(PyCFunctionObject *m, void *closure)
162{
163 /* If __self__ is a module or NULL, return m.__name__
164 (e.g. len.__qualname__ == 'len')
165
166 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
167 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
168
169 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
170 (e.g. [].append.__qualname__ == 'list.append') */
171 PyObject *type, *type_qualname, *res;
172 _Py_IDENTIFIER(__qualname__);
173
174 if (m->m_self == NULL || PyModule_Check(m->m_self))
175 return PyUnicode_FromString(m->m_ml->ml_name);
176
177 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
178
179 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
180 if (type_qualname == NULL)
181 return NULL;
182
183 if (!PyUnicode_Check(type_qualname)) {
184 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
185 "__qualname__ is not a unicode object");
186 Py_XDECREF(type_qualname);
187 return NULL;
188 }
189
190 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
191 Py_DECREF(type_qualname);
192 return res;
193}
194
Neil Schemenauer10c66922001-07-12 13:27:35 +0000195static int
196meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 Py_VISIT(m->m_self);
199 Py_VISIT(m->m_module);
200 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000201}
202
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000207
Antoine Pitrou5b629422011-12-23 12:40:16 +0100208 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (self == NULL)
210 self = Py_None;
211 Py_INCREF(self);
212 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000213}
214
Guido van Rossum32d34c82001-09-20 21:45:26 +0000215static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
217 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100218 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800220 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000222};
223
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000224#define OFF(x) offsetof(PyCFunctionObject, x)
225
226static PyMemberDef meth_members[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100227 {"__module__", T_OBJECT, OFF(m_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000229};
230
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000231static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000232meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 if (m->m_self == NULL || PyModule_Check(m->m_self))
235 return PyUnicode_FromFormat("<built-in function %s>",
236 m->m_ml->ml_name);
237 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
238 m->m_ml->ml_name,
Victor Stinner58ac7002020-02-07 03:04:21 +0100239 Py_TYPE(m->m_self)->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241}
242
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000243static PyObject *
244meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PyCFunctionObject *a, *b;
247 PyObject *res;
248 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if ((op != Py_EQ && op != Py_NE) ||
251 !PyCFunction_Check(self) ||
252 !PyCFunction_Check(other))
253 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500254 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 }
256 a = (PyCFunctionObject *)self;
257 b = (PyCFunctionObject *)other;
258 eq = a->m_self == b->m_self;
259 if (eq)
260 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
261 if (op == Py_EQ)
262 res = eq ? Py_True : Py_False;
263 else
264 res = eq ? Py_False : Py_True;
265 Py_INCREF(res);
266 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267}
268
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000269static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000270meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000271{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000272 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300273 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 x ^= y;
276 if (x == -1)
277 x = -2;
278 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000279}
280
Tim Peters6d6c1a32001-08-02 04:15:00 +0000281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 PyVarObject_HEAD_INIT(&PyType_Type, 0)
284 "builtin_function_or_method",
285 sizeof(PyCFunctionObject),
286 0,
287 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200288 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 0, /* tp_getattr */
290 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200291 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 (reprfunc)meth_repr, /* tp_repr */
293 0, /* tp_as_number */
294 0, /* tp_as_sequence */
295 0, /* tp_as_mapping */
296 (hashfunc)meth_hash, /* tp_hash */
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200297 cfunction_call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 0, /* tp_str */
299 PyObject_GenericGetAttr, /* tp_getattro */
300 0, /* tp_setattro */
301 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200302 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100303 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 0, /* tp_doc */
305 (traverseproc)meth_traverse, /* tp_traverse */
306 0, /* tp_clear */
307 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400308 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 0, /* tp_iter */
310 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800311 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 meth_members, /* tp_members */
313 meth_getsets, /* tp_getset */
314 0, /* tp_base */
315 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000317
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200318/* Vectorcall functions for each of the PyCFunction calling conventions,
319 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
320 * doesn't use vectorcall.
321 *
322 * First, common helpers
323 */
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200324
325static inline int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100326cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200327{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100328 assert(!_PyErr_Occurred(tstate));
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200329 assert(PyCFunction_Check(func));
330 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100331 PyObject *funcstr = _PyObject_FunctionStr(func);
332 if (funcstr != NULL) {
333 _PyErr_Format(tstate, PyExc_TypeError,
334 "%U takes no keyword arguments", funcstr);
335 Py_DECREF(funcstr);
336 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200337 return -1;
338 }
339 return 0;
340}
341
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100342typedef void (*funcptr)(void);
343
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200344static inline funcptr
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100345cfunction_enter_call(PyThreadState *tstate, PyObject *func)
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200346{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100347 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200348 return NULL;
349 }
350 return (funcptr)PyCFunction_GET_FUNCTION(func);
351}
352
353/* Now the actual vectorcall functions */
354static PyObject *
355cfunction_vectorcall_FASTCALL(
356 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
357{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100358 PyThreadState *tstate = _PyThreadState_GET();
359 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200360 return NULL;
361 }
362 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
363 _PyCFunctionFast meth = (_PyCFunctionFast)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100364 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200365 if (meth == NULL) {
366 return NULL;
367 }
368 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100369 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200370 return result;
371}
372
373static PyObject *
374cfunction_vectorcall_FASTCALL_KEYWORDS(
375 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
376{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100377 PyThreadState *tstate = _PyThreadState_GET();
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200378 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
379 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100380 cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200381 if (meth == NULL) {
382 return NULL;
383 }
384 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100385 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200386 return result;
387}
388
389static PyObject *
390cfunction_vectorcall_NOARGS(
391 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
392{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100393 PyThreadState *tstate = _PyThreadState_GET();
394 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200395 return NULL;
396 }
397 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
398 if (nargs != 0) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100399 PyObject *funcstr = _PyObject_FunctionStr(func);
400 if (funcstr != NULL) {
401 _PyErr_Format(tstate, PyExc_TypeError,
402 "%U takes no arguments (%zd given)", funcstr, nargs);
403 Py_DECREF(funcstr);
404 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200405 return NULL;
406 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100407 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200408 if (meth == NULL) {
409 return NULL;
410 }
411 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100412 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200413 return result;
414}
415
416static PyObject *
417cfunction_vectorcall_O(
418 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
419{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100420 PyThreadState *tstate = _PyThreadState_GET();
421 if (cfunction_check_kwargs(tstate, func, kwnames)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200422 return NULL;
423 }
424 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
425 if (nargs != 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100426 PyObject *funcstr = _PyObject_FunctionStr(func);
427 if (funcstr != NULL) {
428 _PyErr_Format(tstate, PyExc_TypeError,
429 "%U takes exactly one argument (%zd given)", funcstr, nargs);
430 Py_DECREF(funcstr);
431 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200432 return NULL;
433 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100434 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200435 if (meth == NULL) {
436 return NULL;
437 }
438 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100439 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200440 return result;
441}
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200442
443
444static PyObject *
445cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
446{
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200447 assert(kwargs == NULL || PyDict_Check(kwargs));
448
Victor Stinner17269092019-11-05 01:22:12 +0100449 PyThreadState *tstate = _PyThreadState_GET();
450 assert(!_PyErr_Occurred(tstate));
451
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200452 int flags = PyCFunction_GET_FLAGS(func);
453 if (!(flags & METH_VARARGS)) {
454 /* If this is not a METH_VARARGS function, delegate to vectorcall */
455 return PyVectorcall_Call(func, args, kwargs);
456 }
457
458 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
459 * is NULL. This is intentional, since vectorcall would be slower. */
460 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
461 PyObject *self = PyCFunction_GET_SELF(func);
462
463 PyObject *result;
464 if (flags & METH_KEYWORDS) {
465 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
466 }
467 else {
468 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner17269092019-11-05 01:22:12 +0100469 _PyErr_Format(tstate, PyExc_TypeError,
470 "%.200s() takes no keyword arguments",
471 ((PyCFunctionObject*)func)->m_ml->ml_name);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200472 return NULL;
473 }
474 result = meth(self, args);
475 }
Victor Stinner17269092019-11-05 01:22:12 +0100476 return _Py_CheckFunctionResult(tstate, func, result, NULL);
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200477}