blob: 3494f11d80fe75967ce0601b08ab4e1ce1598963 [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
Christian Heimes2202f872008-02-06 14:31:34 +000010/* Free list for method objects to safe malloc/free overhead
11 * The m_self element is used to chain the objects.
12 */
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000013static PyCFunctionObject *free_list = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +000014static int numfree = 0;
15#ifndef PyCFunction_MAXFREELIST
16#define PyCFunction_MAXFREELIST 256
17#endif
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000018
Andrew Svetlov4de29242012-12-26 23:08:54 +020019/* undefine macro trampoline to PyCFunction_NewEx */
20#undef PyCFunction_New
21
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020022/* Forward declarations */
23static PyObject * cfunction_vectorcall_FASTCALL(
24 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
25static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
26 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
27static PyObject * cfunction_vectorcall_NOARGS(
28 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
29static PyObject * cfunction_vectorcall_O(
30 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
31
32
Benjamin Petersone5024512018-09-12 12:06:42 -070033PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020034PyCFunction_New(PyMethodDef *ml, PyObject *self)
35{
36 return PyCFunction_NewEx(ml, self, NULL);
37}
38
39PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000040PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041{
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020042 /* Figure out correct vectorcall function to use */
43 vectorcallfunc vectorcall;
44 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS))
45 {
46 case METH_VARARGS:
47 case METH_VARARGS | METH_KEYWORDS:
48 /* For METH_VARARGS functions, it's more efficient to use tp_call
49 * instead of vectorcall. */
50 vectorcall = NULL;
51 break;
52 case METH_FASTCALL:
53 vectorcall = cfunction_vectorcall_FASTCALL;
54 break;
55 case METH_FASTCALL | METH_KEYWORDS:
56 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
57 break;
58 case METH_NOARGS:
59 vectorcall = cfunction_vectorcall_NOARGS;
60 break;
61 case METH_O:
62 vectorcall = cfunction_vectorcall_O;
63 break;
64 default:
65 PyErr_SetString(PyExc_SystemError, "bad call flags");
66 return NULL;
67 }
68
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 PyCFunctionObject *op;
70 op = free_list;
71 if (op != NULL) {
72 free_list = (PyCFunctionObject *)(op->m_self);
Victor Stinnerb509d522018-11-23 14:27:38 +010073 (void)PyObject_INIT(op, &PyCFunction_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 numfree--;
75 }
76 else {
77 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
78 if (op == NULL)
79 return NULL;
80 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040081 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 op->m_ml = ml;
83 Py_XINCREF(self);
84 op->m_self = self;
85 Py_XINCREF(module);
86 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020087 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 _PyObject_GC_TRACK(op);
89 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000093PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (!PyCFunction_Check(op)) {
96 PyErr_BadInternalCall();
97 return NULL;
98 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010099 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000103PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (!PyCFunction_Check(op)) {
106 PyErr_BadInternalCall();
107 return NULL;
108 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100109 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110}
111
Guido van Rossumc0602291991-12-16 13:07:24 +0000112int
Fred Drakeee238b92000-07-09 06:03:25 +0000113PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (!PyCFunction_Check(op)) {
116 PyErr_BadInternalCall();
117 return -1;
118 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100119 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000120}
121
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122/* Methods (the standard built-in methods, that is) */
123
124static void
Fred Drakeee238b92000-07-09 06:03:25 +0000125meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400128 if (m->m_weakreflist != NULL) {
129 PyObject_ClearWeakRefs((PyObject*) m);
130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 Py_XDECREF(m->m_self);
132 Py_XDECREF(m->m_module);
133 if (numfree < PyCFunction_MAXFREELIST) {
134 m->m_self = (PyObject *)free_list;
135 free_list = m;
136 numfree++;
137 }
138 else {
139 PyObject_GC_Del(m);
140 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800143static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530144meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800145{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800146 _Py_IDENTIFIER(getattr);
147
148 if (m->m_self == NULL || PyModule_Check(m->m_self))
149 return PyUnicode_FromString(m->m_ml->ml_name);
150
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200151 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
152 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800153}
154
155static PyMethodDef meth_methods[] = {
156 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
157 {NULL, NULL}
158};
159
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800160static PyObject *
161meth_get__text_signature__(PyCFunctionObject *m, void *closure)
162{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800163 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800164}
165
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166static PyObject *
167meth_get__doc__(PyCFunctionObject *m, void *closure)
168{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800169 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170}
171
172static PyObject *
173meth_get__name__(PyCFunctionObject *m, void *closure)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176}
177
Antoine Pitrou5b629422011-12-23 12:40:16 +0100178static PyObject *
179meth_get__qualname__(PyCFunctionObject *m, void *closure)
180{
181 /* If __self__ is a module or NULL, return m.__name__
182 (e.g. len.__qualname__ == 'len')
183
184 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
185 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
186
187 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
188 (e.g. [].append.__qualname__ == 'list.append') */
189 PyObject *type, *type_qualname, *res;
190 _Py_IDENTIFIER(__qualname__);
191
192 if (m->m_self == NULL || PyModule_Check(m->m_self))
193 return PyUnicode_FromString(m->m_ml->ml_name);
194
195 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
196
197 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
198 if (type_qualname == NULL)
199 return NULL;
200
201 if (!PyUnicode_Check(type_qualname)) {
202 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
203 "__qualname__ is not a unicode object");
204 Py_XDECREF(type_qualname);
205 return NULL;
206 }
207
208 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
209 Py_DECREF(type_qualname);
210 return res;
211}
212
Neil Schemenauer10c66922001-07-12 13:27:35 +0000213static int
214meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 Py_VISIT(m->m_self);
217 Py_VISIT(m->m_module);
218 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000219}
220
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000222meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000225
Antoine Pitrou5b629422011-12-23 12:40:16 +0100226 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (self == NULL)
228 self = Py_None;
229 Py_INCREF(self);
230 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000231}
232
Guido van Rossum32d34c82001-09-20 21:45:26 +0000233static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
235 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100236 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800238 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000240};
241
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000242#define OFF(x) offsetof(PyCFunctionObject, x)
243
244static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
246 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000247};
248
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000250meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (m->m_self == NULL || PyModule_Check(m->m_self))
253 return PyUnicode_FromFormat("<built-in function %s>",
254 m->m_ml->ml_name);
255 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
256 m->m_ml->ml_name,
257 m->m_self->ob_type->tp_name,
258 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000261static PyObject *
262meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PyCFunctionObject *a, *b;
265 PyObject *res;
266 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if ((op != Py_EQ && op != Py_NE) ||
269 !PyCFunction_Check(self) ||
270 !PyCFunction_Check(other))
271 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500272 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 }
274 a = (PyCFunctionObject *)self;
275 b = (PyCFunctionObject *)other;
276 eq = a->m_self == b->m_self;
277 if (eq)
278 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
279 if (op == Py_EQ)
280 res = eq ? Py_True : Py_False;
281 else
282 res = eq ? Py_False : Py_True;
283 Py_INCREF(res);
284 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000285}
286
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000287static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000288meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000289{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000290 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300291 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 x ^= y;
294 if (x == -1)
295 x = -2;
296 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000297}
298
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 PyVarObject_HEAD_INIT(&PyType_Type, 0)
302 "builtin_function_or_method",
303 sizeof(PyCFunctionObject),
304 0,
305 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200306 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 0, /* tp_getattr */
308 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200309 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 (reprfunc)meth_repr, /* tp_repr */
311 0, /* tp_as_number */
312 0, /* tp_as_sequence */
313 0, /* tp_as_mapping */
314 (hashfunc)meth_hash, /* tp_hash */
315 PyCFunction_Call, /* tp_call */
316 0, /* tp_str */
317 PyObject_GenericGetAttr, /* tp_getattro */
318 0, /* tp_setattro */
319 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200320 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
321 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 0, /* tp_doc */
323 (traverseproc)meth_traverse, /* tp_traverse */
324 0, /* tp_clear */
325 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400326 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 0, /* tp_iter */
328 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800329 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 meth_members, /* tp_members */
331 meth_getsets, /* tp_getset */
332 0, /* tp_base */
333 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000336/* Clear out the free list */
337
Christian Heimesa156e092008-02-16 07:38:31 +0000338int
339PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 int freelist_size = numfree;
342
343 while (free_list) {
344 PyCFunctionObject *v = free_list;
345 free_list = (PyCFunctionObject *)(v->m_self);
346 PyObject_GC_Del(v);
347 numfree--;
348 }
349 assert(numfree == 0);
350 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000351}
352
353void
354PyCFunction_Fini(void)
355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000357}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000358
David Malcolm49526f42012-06-22 14:55:41 -0400359/* Print summary info about the state of the optimized allocator */
360void
361_PyCFunction_DebugMallocStats(FILE *out)
362{
363 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200364 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100365 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400366}
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200367
368
369/* Vectorcall functions for each of the PyCFunction calling conventions,
370 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
371 * doesn't use vectorcall.
372 *
373 * First, common helpers
374 */
375static const char *
376get_name(PyObject *func)
377{
378 assert(PyCFunction_Check(func));
379 PyMethodDef *method = ((PyCFunctionObject *)func)->m_ml;
380 return method->ml_name;
381}
382
383typedef void (*funcptr)(void);
384
385static inline int
386cfunction_check_kwargs(PyObject *func, PyObject *kwnames)
387{
388 assert(!PyErr_Occurred());
389 assert(PyCFunction_Check(func));
390 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
391 PyErr_Format(PyExc_TypeError,
392 "%.200s() takes no keyword arguments", get_name(func));
393 return -1;
394 }
395 return 0;
396}
397
398static inline funcptr
399cfunction_enter_call(PyObject *func)
400{
401 if (Py_EnterRecursiveCall(" while calling a Python object")) {
402 return NULL;
403 }
404 return (funcptr)PyCFunction_GET_FUNCTION(func);
405}
406
407/* Now the actual vectorcall functions */
408static PyObject *
409cfunction_vectorcall_FASTCALL(
410 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
411{
412 if (cfunction_check_kwargs(func, kwnames)) {
413 return NULL;
414 }
415 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
416 _PyCFunctionFast meth = (_PyCFunctionFast)
417 cfunction_enter_call(func);
418 if (meth == NULL) {
419 return NULL;
420 }
421 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
422 Py_LeaveRecursiveCall();
423 return result;
424}
425
426static PyObject *
427cfunction_vectorcall_FASTCALL_KEYWORDS(
428 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
429{
430 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
431 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
432 cfunction_enter_call(func);
433 if (meth == NULL) {
434 return NULL;
435 }
436 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
437 Py_LeaveRecursiveCall();
438 return result;
439}
440
441static PyObject *
442cfunction_vectorcall_NOARGS(
443 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
444{
445 if (cfunction_check_kwargs(func, kwnames)) {
446 return NULL;
447 }
448 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
449 if (nargs != 0) {
450 PyErr_Format(PyExc_TypeError,
451 "%.200s() takes no arguments (%zd given)", get_name(func), nargs);
452 return NULL;
453 }
454 PyCFunction meth = (PyCFunction)cfunction_enter_call(func);
455 if (meth == NULL) {
456 return NULL;
457 }
458 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
459 Py_LeaveRecursiveCall();
460 return result;
461}
462
463static PyObject *
464cfunction_vectorcall_O(
465 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
466{
467 if (cfunction_check_kwargs(func, kwnames)) {
468 return NULL;
469 }
470 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
471 if (nargs != 1) {
472 PyErr_Format(PyExc_TypeError,
473 "%.200s() takes exactly one argument (%zd given)",
474 get_name(func), nargs);
475 return NULL;
476 }
477 PyCFunction meth = (PyCFunction)cfunction_enter_call(func);
478 if (meth == NULL) {
479 return NULL;
480 }
481 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
482 Py_LeaveRecursiveCall();
483 return result;
484}