1
2 /* Method object implementation */
3
4 #include "Python.h"
5 #include "pycore_call.h" // _Py_CheckFunctionResult()
6 #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
7 #include "pycore_object.h"
8 #include "pycore_pyerrors.h"
9 #include "pycore_pystate.h" // _PyThreadState_GET()
10
11
12 /* undefine macro trampoline to PyCFunction_NewEx */
13 #undef PyCFunction_New
14 /* undefine macro trampoline to PyCMethod_New */
15 #undef PyCFunction_NewEx
16
17 /* Forward declarations */
18 static PyObject * cfunction_vectorcall_FASTCALL(
19 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
20 static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
21 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
22 static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
23 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
24 static PyObject * cfunction_vectorcall_NOARGS(
25 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
26 static PyObject * cfunction_vectorcall_O(
27 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
28 static PyObject * cfunction_call(
29 PyObject *func, PyObject *args, PyObject *kwargs);
30
31
32 PyObject *
PyCFunction_New(PyMethodDef * ml,PyObject * self)33 PyCFunction_New(PyMethodDef *ml, PyObject *self)
34 {
35 return PyCFunction_NewEx(ml, self, NULL);
36 }
37
38 PyObject *
PyCFunction_NewEx(PyMethodDef * ml,PyObject * self,PyObject * module)39 PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
40 {
41 return PyCMethod_New(ml, self, module, NULL);
42 }
43
44 PyObject *
PyCMethod_New(PyMethodDef * ml,PyObject * self,PyObject * module,PyTypeObject * cls)45 PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *cls)
46 {
47 /* Figure out correct vectorcall function to use */
48 vectorcallfunc vectorcall;
49 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS |
50 METH_O | METH_KEYWORDS | METH_METHOD))
51 {
52 case METH_VARARGS:
53 case METH_VARARGS | METH_KEYWORDS:
54 /* For METH_VARARGS functions, it's more efficient to use tp_call
55 * instead of vectorcall. */
56 vectorcall = NULL;
57 break;
58 case METH_FASTCALL:
59 vectorcall = cfunction_vectorcall_FASTCALL;
60 break;
61 case METH_FASTCALL | METH_KEYWORDS:
62 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
63 break;
64 case METH_NOARGS:
65 vectorcall = cfunction_vectorcall_NOARGS;
66 break;
67 case METH_O:
68 vectorcall = cfunction_vectorcall_O;
69 break;
70 case METH_METHOD | METH_FASTCALL | METH_KEYWORDS:
71 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD;
72 break;
73 default:
74 PyErr_Format(PyExc_SystemError,
75 "%s() method: bad call flags", ml->ml_name);
76 return NULL;
77 }
78
79 PyCFunctionObject *op = NULL;
80
81 if (ml->ml_flags & METH_METHOD) {
82 if (!cls) {
83 PyErr_SetString(PyExc_SystemError,
84 "attempting to create PyCMethod with a METH_METHOD "
85 "flag but no class");
86 return NULL;
87 }
88 PyCMethodObject *om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
89 if (om == NULL) {
90 return NULL;
91 }
92 om->mm_class = (PyTypeObject*)Py_NewRef(cls);
93 op = (PyCFunctionObject *)om;
94 } else {
95 if (cls) {
96 PyErr_SetString(PyExc_SystemError,
97 "attempting to create PyCFunction with class "
98 "but no METH_METHOD flag");
99 return NULL;
100 }
101 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
102 if (op == NULL) {
103 return NULL;
104 }
105 }
106
107 op->m_weakreflist = NULL;
108 op->m_ml = ml;
109 op->m_self = Py_XNewRef(self);
110 op->m_module = Py_XNewRef(module);
111 op->vectorcall = vectorcall;
112 _PyObject_GC_TRACK(op);
113 return (PyObject *)op;
114 }
115
116 PyCFunction
PyCFunction_GetFunction(PyObject * op)117 PyCFunction_GetFunction(PyObject *op)
118 {
119 if (!PyCFunction_Check(op)) {
120 PyErr_BadInternalCall();
121 return NULL;
122 }
123 return PyCFunction_GET_FUNCTION(op);
124 }
125
126 PyObject *
PyCFunction_GetSelf(PyObject * op)127 PyCFunction_GetSelf(PyObject *op)
128 {
129 if (!PyCFunction_Check(op)) {
130 PyErr_BadInternalCall();
131 return NULL;
132 }
133 return PyCFunction_GET_SELF(op);
134 }
135
136 int
PyCFunction_GetFlags(PyObject * op)137 PyCFunction_GetFlags(PyObject *op)
138 {
139 if (!PyCFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return -1;
142 }
143 return PyCFunction_GET_FLAGS(op);
144 }
145
146 PyTypeObject *
PyCMethod_GetClass(PyObject * op)147 PyCMethod_GetClass(PyObject *op)
148 {
149 if (!PyCFunction_Check(op)) {
150 PyErr_BadInternalCall();
151 return NULL;
152 }
153 return PyCFunction_GET_CLASS(op);
154 }
155
156 /* Methods (the standard built-in methods, that is) */
157
158 static void
meth_dealloc(PyCFunctionObject * m)159 meth_dealloc(PyCFunctionObject *m)
160 {
161 // The Py_TRASHCAN mechanism requires that we be able to
162 // call PyObject_GC_UnTrack twice on an object.
163 PyObject_GC_UnTrack(m);
164 Py_TRASHCAN_BEGIN(m, meth_dealloc);
165 if (m->m_weakreflist != NULL) {
166 PyObject_ClearWeakRefs((PyObject*) m);
167 }
168 // Dereference class before m_self: PyCFunction_GET_CLASS accesses
169 // PyMethodDef m_ml, which could be kept alive by m_self
170 Py_XDECREF(PyCFunction_GET_CLASS(m));
171 Py_XDECREF(m->m_self);
172 Py_XDECREF(m->m_module);
173 PyObject_GC_Del(m);
174 Py_TRASHCAN_END;
175 }
176
177 static PyObject *
meth_reduce(PyCFunctionObject * m,PyObject * Py_UNUSED (ignored))178 meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
179 {
180 if (m->m_self == NULL || PyModule_Check(m->m_self))
181 return PyUnicode_FromString(m->m_ml->ml_name);
182
183 return Py_BuildValue("N(Os)", _PyEval_GetBuiltin(&_Py_ID(getattr)),
184 m->m_self, m->m_ml->ml_name);
185 }
186
187 static PyMethodDef meth_methods[] = {
188 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
189 {NULL, NULL}
190 };
191
192 static PyObject *
meth_get__text_signature__(PyCFunctionObject * m,void * closure)193 meth_get__text_signature__(PyCFunctionObject *m, void *closure)
194 {
195 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name,
196 m->m_ml->ml_doc,
197 m->m_ml->ml_flags);
198 }
199
200 static PyObject *
meth_get__doc__(PyCFunctionObject * m,void * closure)201 meth_get__doc__(PyCFunctionObject *m, void *closure)
202 {
203 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
204 }
205
206 static PyObject *
meth_get__name__(PyCFunctionObject * m,void * closure)207 meth_get__name__(PyCFunctionObject *m, void *closure)
208 {
209 return PyUnicode_FromString(m->m_ml->ml_name);
210 }
211
212 static PyObject *
meth_get__qualname__(PyCFunctionObject * m,void * closure)213 meth_get__qualname__(PyCFunctionObject *m, void *closure)
214 {
215 /* If __self__ is a module or NULL, return m.__name__
216 (e.g. len.__qualname__ == 'len')
217
218 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
219 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
220
221 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
222 (e.g. [].append.__qualname__ == 'list.append') */
223 PyObject *type, *type_qualname, *res;
224
225 if (m->m_self == NULL || PyModule_Check(m->m_self))
226 return PyUnicode_FromString(m->m_ml->ml_name);
227
228 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
229
230 type_qualname = PyObject_GetAttr(type, &_Py_ID(__qualname__));
231 if (type_qualname == NULL)
232 return NULL;
233
234 if (!PyUnicode_Check(type_qualname)) {
235 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
236 "__qualname__ is not a unicode object");
237 Py_XDECREF(type_qualname);
238 return NULL;
239 }
240
241 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
242 Py_DECREF(type_qualname);
243 return res;
244 }
245
246 static int
meth_traverse(PyCFunctionObject * m,visitproc visit,void * arg)247 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
248 {
249 Py_VISIT(PyCFunction_GET_CLASS(m));
250 Py_VISIT(m->m_self);
251 Py_VISIT(m->m_module);
252 return 0;
253 }
254
255 static PyObject *
meth_get__self__(PyCFunctionObject * m,void * closure)256 meth_get__self__(PyCFunctionObject *m, void *closure)
257 {
258 PyObject *self;
259
260 self = PyCFunction_GET_SELF(m);
261 if (self == NULL)
262 self = Py_None;
263 return Py_NewRef(self);
264 }
265
266 static PyGetSetDef meth_getsets [] = {
267 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
268 {"__name__", (getter)meth_get__name__, NULL, NULL},
269 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
270 {"__self__", (getter)meth_get__self__, NULL, NULL},
271 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
272 {0}
273 };
274
275 #define OFF(x) offsetof(PyCFunctionObject, x)
276
277 static PyMemberDef meth_members[] = {
278 {"__module__", _Py_T_OBJECT, OFF(m_module), 0},
279 {NULL}
280 };
281
282 static PyObject *
meth_repr(PyCFunctionObject * m)283 meth_repr(PyCFunctionObject *m)
284 {
285 if (m->m_self == NULL || PyModule_Check(m->m_self))
286 return PyUnicode_FromFormat("<built-in function %s>",
287 m->m_ml->ml_name);
288 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
289 m->m_ml->ml_name,
290 Py_TYPE(m->m_self)->tp_name,
291 m->m_self);
292 }
293
294 static PyObject *
meth_richcompare(PyObject * self,PyObject * other,int op)295 meth_richcompare(PyObject *self, PyObject *other, int op)
296 {
297 PyCFunctionObject *a, *b;
298 PyObject *res;
299 int eq;
300
301 if ((op != Py_EQ && op != Py_NE) ||
302 !PyCFunction_Check(self) ||
303 !PyCFunction_Check(other))
304 {
305 Py_RETURN_NOTIMPLEMENTED;
306 }
307 a = (PyCFunctionObject *)self;
308 b = (PyCFunctionObject *)other;
309 eq = a->m_self == b->m_self;
310 if (eq)
311 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
312 if (op == Py_EQ)
313 res = eq ? Py_True : Py_False;
314 else
315 res = eq ? Py_False : Py_True;
316 return Py_NewRef(res);
317 }
318
319 static Py_hash_t
meth_hash(PyCFunctionObject * a)320 meth_hash(PyCFunctionObject *a)
321 {
322 Py_hash_t x, y;
323 x = PyObject_GenericHash(a->m_self);
324 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
325 x ^= y;
326 if (x == -1)
327 x = -2;
328 return x;
329 }
330
331
332 PyTypeObject PyCFunction_Type = {
333 PyVarObject_HEAD_INIT(&PyType_Type, 0)
334 "builtin_function_or_method",
335 sizeof(PyCFunctionObject),
336 0,
337 (destructor)meth_dealloc, /* tp_dealloc */
338 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
339 0, /* tp_getattr */
340 0, /* tp_setattr */
341 0, /* tp_as_async */
342 (reprfunc)meth_repr, /* tp_repr */
343 0, /* tp_as_number */
344 0, /* tp_as_sequence */
345 0, /* tp_as_mapping */
346 (hashfunc)meth_hash, /* tp_hash */
347 cfunction_call, /* tp_call */
348 0, /* tp_str */
349 PyObject_GenericGetAttr, /* tp_getattro */
350 0, /* tp_setattro */
351 0, /* tp_as_buffer */
352 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
353 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
354 0, /* tp_doc */
355 (traverseproc)meth_traverse, /* tp_traverse */
356 0, /* tp_clear */
357 meth_richcompare, /* tp_richcompare */
358 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
359 0, /* tp_iter */
360 0, /* tp_iternext */
361 meth_methods, /* tp_methods */
362 meth_members, /* tp_members */
363 meth_getsets, /* tp_getset */
364 0, /* tp_base */
365 0, /* tp_dict */
366 };
367
368 PyTypeObject PyCMethod_Type = {
369 PyVarObject_HEAD_INIT(&PyType_Type, 0)
370 .tp_name = "builtin_method",
371 .tp_basicsize = sizeof(PyCMethodObject),
372 .tp_base = &PyCFunction_Type,
373 };
374
375 /* Vectorcall functions for each of the PyCFunction calling conventions,
376 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
377 * doesn't use vectorcall.
378 *
379 * First, common helpers
380 */
381
382 static inline int
cfunction_check_kwargs(PyThreadState * tstate,PyObject * func,PyObject * kwnames)383 cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
384 {
385 assert(!_PyErr_Occurred(tstate));
386 assert(PyCFunction_Check(func));
387 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
388 PyObject *funcstr = _PyObject_FunctionStr(func);
389 if (funcstr != NULL) {
390 _PyErr_Format(tstate, PyExc_TypeError,
391 "%U takes no keyword arguments", funcstr);
392 Py_DECREF(funcstr);
393 }
394 return -1;
395 }
396 return 0;
397 }
398
399 typedef void (*funcptr)(void);
400
401 static inline funcptr
cfunction_enter_call(PyThreadState * tstate,PyObject * func)402 cfunction_enter_call(PyThreadState *tstate, PyObject *func)
403 {
404 if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
405 return NULL;
406 }
407 return (funcptr)PyCFunction_GET_FUNCTION(func);
408 }
409
410 /* Now the actual vectorcall functions */
411 static PyObject *
cfunction_vectorcall_FASTCALL(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)412 cfunction_vectorcall_FASTCALL(
413 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
414 {
415 PyThreadState *tstate = _PyThreadState_GET();
416 if (cfunction_check_kwargs(tstate, func, kwnames)) {
417 return NULL;
418 }
419 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
420 PyCFunctionFast meth = (PyCFunctionFast)
421 cfunction_enter_call(tstate, func);
422 if (meth == NULL) {
423 return NULL;
424 }
425 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
426 _Py_LeaveRecursiveCallTstate(tstate);
427 return result;
428 }
429
430 static PyObject *
cfunction_vectorcall_FASTCALL_KEYWORDS(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)431 cfunction_vectorcall_FASTCALL_KEYWORDS(
432 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
433 {
434 PyThreadState *tstate = _PyThreadState_GET();
435 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
436 PyCFunctionFastWithKeywords meth = (PyCFunctionFastWithKeywords)
437 cfunction_enter_call(tstate, func);
438 if (meth == NULL) {
439 return NULL;
440 }
441 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
442 _Py_LeaveRecursiveCallTstate(tstate);
443 return result;
444 }
445
446 static PyObject *
cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)447 cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
448 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
449 {
450 PyThreadState *tstate = _PyThreadState_GET();
451 PyTypeObject *cls = PyCFunction_GET_CLASS(func);
452 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
453 PyCMethod meth = (PyCMethod)cfunction_enter_call(tstate, func);
454 if (meth == NULL) {
455 return NULL;
456 }
457 PyObject *result = meth(PyCFunction_GET_SELF(func), cls, args, nargs, kwnames);
458 _Py_LeaveRecursiveCallTstate(tstate);
459 return result;
460 }
461
462 static PyObject *
cfunction_vectorcall_NOARGS(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)463 cfunction_vectorcall_NOARGS(
464 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
465 {
466 PyThreadState *tstate = _PyThreadState_GET();
467 if (cfunction_check_kwargs(tstate, func, kwnames)) {
468 return NULL;
469 }
470 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
471 if (nargs != 0) {
472 PyObject *funcstr = _PyObject_FunctionStr(func);
473 if (funcstr != NULL) {
474 _PyErr_Format(tstate, PyExc_TypeError,
475 "%U takes no arguments (%zd given)", funcstr, nargs);
476 Py_DECREF(funcstr);
477 }
478 return NULL;
479 }
480 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
481 if (meth == NULL) {
482 return NULL;
483 }
484 PyObject *result = _PyCFunction_TrampolineCall(
485 meth, PyCFunction_GET_SELF(func), NULL);
486 _Py_LeaveRecursiveCallTstate(tstate);
487 return result;
488 }
489
490 static PyObject *
cfunction_vectorcall_O(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)491 cfunction_vectorcall_O(
492 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
493 {
494 PyThreadState *tstate = _PyThreadState_GET();
495 if (cfunction_check_kwargs(tstate, func, kwnames)) {
496 return NULL;
497 }
498 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
499 if (nargs != 1) {
500 PyObject *funcstr = _PyObject_FunctionStr(func);
501 if (funcstr != NULL) {
502 _PyErr_Format(tstate, PyExc_TypeError,
503 "%U takes exactly one argument (%zd given)", funcstr, nargs);
504 Py_DECREF(funcstr);
505 }
506 return NULL;
507 }
508 PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
509 if (meth == NULL) {
510 return NULL;
511 }
512 PyObject *result = _PyCFunction_TrampolineCall(
513 meth, PyCFunction_GET_SELF(func), args[0]);
514 _Py_LeaveRecursiveCallTstate(tstate);
515 return result;
516 }
517
518
519 static PyObject *
cfunction_call(PyObject * func,PyObject * args,PyObject * kwargs)520 cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
521 {
522 assert(kwargs == NULL || PyDict_Check(kwargs));
523
524 PyThreadState *tstate = _PyThreadState_GET();
525 assert(!_PyErr_Occurred(tstate));
526
527 int flags = PyCFunction_GET_FLAGS(func);
528 if (!(flags & METH_VARARGS)) {
529 /* If this is not a METH_VARARGS function, delegate to vectorcall */
530 return PyVectorcall_Call(func, args, kwargs);
531 }
532
533 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
534 * is NULL. This is intentional, since vectorcall would be slower. */
535 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
536 PyObject *self = PyCFunction_GET_SELF(func);
537
538 PyObject *result;
539 if (flags & METH_KEYWORDS) {
540 result = _PyCFunctionWithKeywords_TrampolineCall(
541 (*(PyCFunctionWithKeywords)(void(*)(void))meth),
542 self, args, kwargs);
543 }
544 else {
545 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
546 _PyErr_Format(tstate, PyExc_TypeError,
547 "%.200s() takes no keyword arguments",
548 ((PyCFunctionObject*)func)->m_ml->ml_name);
549 return NULL;
550 }
551 result = _PyCFunction_TrampolineCall(meth, self, args);
552 }
553 return _Py_CheckFunctionResult(tstate, func, result, NULL);
554 }
555