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