• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // The Py_TRASHCAN mechanism requires that we be able to
164     // call PyObject_GC_UnTrack twice on an object.
165     PyObject_GC_UnTrack(m);
166     Py_TRASHCAN_BEGIN(m, meth_dealloc);
167     if (m->m_weakreflist != NULL) {
168         PyObject_ClearWeakRefs((PyObject*) m);
169     }
170     // Dereference class before m_self: PyCFunction_GET_CLASS accesses
171     // PyMethodDef m_ml, which could be kept alive by m_self
172     Py_XDECREF(PyCFunction_GET_CLASS(m));
173     Py_XDECREF(m->m_self);
174     Py_XDECREF(m->m_module);
175     PyObject_GC_Del(m);
176     Py_TRASHCAN_END;
177 }
178 
179 static PyObject *
meth_reduce(PyCFunctionObject * m,PyObject * Py_UNUSED (ignored))180 meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
181 {
182     _Py_IDENTIFIER(getattr);
183 
184     if (m->m_self == NULL || PyModule_Check(m->m_self))
185         return PyUnicode_FromString(m->m_ml->ml_name);
186 
187     return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
188                          m->m_self, m->m_ml->ml_name);
189 }
190 
191 static PyMethodDef meth_methods[] = {
192     {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
193     {NULL, NULL}
194 };
195 
196 static PyObject *
meth_get__text_signature__(PyCFunctionObject * m,void * closure)197 meth_get__text_signature__(PyCFunctionObject *m, void *closure)
198 {
199     return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
200 }
201 
202 static PyObject *
meth_get__doc__(PyCFunctionObject * m,void * closure)203 meth_get__doc__(PyCFunctionObject *m, void *closure)
204 {
205     return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
206 }
207 
208 static PyObject *
meth_get__name__(PyCFunctionObject * m,void * closure)209 meth_get__name__(PyCFunctionObject *m, void *closure)
210 {
211     return PyUnicode_FromString(m->m_ml->ml_name);
212 }
213 
214 static PyObject *
meth_get__qualname__(PyCFunctionObject * m,void * closure)215 meth_get__qualname__(PyCFunctionObject *m, void *closure)
216 {
217     /* If __self__ is a module or NULL, return m.__name__
218        (e.g. len.__qualname__ == 'len')
219 
220        If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
221        (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
222 
223        Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
224        (e.g. [].append.__qualname__ == 'list.append') */
225     PyObject *type, *type_qualname, *res;
226     _Py_IDENTIFIER(__qualname__);
227 
228     if (m->m_self == NULL || PyModule_Check(m->m_self))
229         return PyUnicode_FromString(m->m_ml->ml_name);
230 
231     type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
232 
233     type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
234     if (type_qualname == NULL)
235         return NULL;
236 
237     if (!PyUnicode_Check(type_qualname)) {
238         PyErr_SetString(PyExc_TypeError, "<method>.__class__."
239                         "__qualname__ is not a unicode object");
240         Py_XDECREF(type_qualname);
241         return NULL;
242     }
243 
244     res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
245     Py_DECREF(type_qualname);
246     return res;
247 }
248 
249 static int
meth_traverse(PyCFunctionObject * m,visitproc visit,void * arg)250 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
251 {
252     Py_VISIT(PyCFunction_GET_CLASS(m));
253     Py_VISIT(m->m_self);
254     Py_VISIT(m->m_module);
255     return 0;
256 }
257 
258 static PyObject *
meth_get__self__(PyCFunctionObject * m,void * closure)259 meth_get__self__(PyCFunctionObject *m, void *closure)
260 {
261     PyObject *self;
262 
263     self = PyCFunction_GET_SELF(m);
264     if (self == NULL)
265         self = Py_None;
266     Py_INCREF(self);
267     return self;
268 }
269 
270 static PyGetSetDef meth_getsets [] = {
271     {"__doc__",  (getter)meth_get__doc__,  NULL, NULL},
272     {"__name__", (getter)meth_get__name__, NULL, NULL},
273     {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
274     {"__self__", (getter)meth_get__self__, NULL, NULL},
275     {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
276     {0}
277 };
278 
279 #define OFF(x) offsetof(PyCFunctionObject, x)
280 
281 static PyMemberDef meth_members[] = {
282     {"__module__",    T_OBJECT,     OFF(m_module), 0},
283     {NULL}
284 };
285 
286 static PyObject *
meth_repr(PyCFunctionObject * m)287 meth_repr(PyCFunctionObject *m)
288 {
289     if (m->m_self == NULL || PyModule_Check(m->m_self))
290         return PyUnicode_FromFormat("<built-in function %s>",
291                                    m->m_ml->ml_name);
292     return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
293                                m->m_ml->ml_name,
294                                Py_TYPE(m->m_self)->tp_name,
295                                m->m_self);
296 }
297 
298 static PyObject *
meth_richcompare(PyObject * self,PyObject * other,int op)299 meth_richcompare(PyObject *self, PyObject *other, int op)
300 {
301     PyCFunctionObject *a, *b;
302     PyObject *res;
303     int eq;
304 
305     if ((op != Py_EQ && op != Py_NE) ||
306         !PyCFunction_Check(self) ||
307         !PyCFunction_Check(other))
308     {
309         Py_RETURN_NOTIMPLEMENTED;
310     }
311     a = (PyCFunctionObject *)self;
312     b = (PyCFunctionObject *)other;
313     eq = a->m_self == b->m_self;
314     if (eq)
315         eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
316     if (op == Py_EQ)
317         res = eq ? Py_True : Py_False;
318     else
319         res = eq ? Py_False : Py_True;
320     Py_INCREF(res);
321     return res;
322 }
323 
324 static Py_hash_t
meth_hash(PyCFunctionObject * a)325 meth_hash(PyCFunctionObject *a)
326 {
327     Py_hash_t x, y;
328     x = _Py_HashPointer(a->m_self);
329     y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
330     x ^= y;
331     if (x == -1)
332         x = -2;
333     return x;
334 }
335 
336 
337 PyTypeObject PyCFunction_Type = {
338     PyVarObject_HEAD_INIT(&PyType_Type, 0)
339     "builtin_function_or_method",
340     sizeof(PyCFunctionObject),
341     0,
342     (destructor)meth_dealloc,                   /* tp_dealloc */
343     offsetof(PyCFunctionObject, vectorcall),    /* tp_vectorcall_offset */
344     0,                                          /* tp_getattr */
345     0,                                          /* tp_setattr */
346     0,                                          /* tp_as_async */
347     (reprfunc)meth_repr,                        /* tp_repr */
348     0,                                          /* tp_as_number */
349     0,                                          /* tp_as_sequence */
350     0,                                          /* tp_as_mapping */
351     (hashfunc)meth_hash,                        /* tp_hash */
352     cfunction_call,                             /* tp_call */
353     0,                                          /* tp_str */
354     PyObject_GenericGetAttr,                    /* tp_getattro */
355     0,                                          /* tp_setattro */
356     0,                                          /* tp_as_buffer */
357     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
358     Py_TPFLAGS_HAVE_VECTORCALL,                 /* tp_flags */
359     0,                                          /* tp_doc */
360     (traverseproc)meth_traverse,                /* tp_traverse */
361     0,                                          /* tp_clear */
362     meth_richcompare,                           /* tp_richcompare */
363     offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
364     0,                                          /* tp_iter */
365     0,                                          /* tp_iternext */
366     meth_methods,                               /* tp_methods */
367     meth_members,                               /* tp_members */
368     meth_getsets,                               /* tp_getset */
369     0,                                          /* tp_base */
370     0,                                          /* tp_dict */
371 };
372 
373 PyTypeObject PyCMethod_Type = {
374     PyVarObject_HEAD_INIT(&PyType_Type, 0)
375     .tp_name = "builtin_method",
376     .tp_basicsize = sizeof(PyCMethodObject),
377     .tp_base = &PyCFunction_Type,
378 };
379 
380 /* Vectorcall functions for each of the PyCFunction calling conventions,
381  * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
382  * doesn't use vectorcall.
383  *
384  * First, common helpers
385  */
386 
387 static inline int
cfunction_check_kwargs(PyThreadState * tstate,PyObject * func,PyObject * kwnames)388 cfunction_check_kwargs(PyThreadState *tstate, PyObject *func, PyObject *kwnames)
389 {
390     assert(!_PyErr_Occurred(tstate));
391     assert(PyCFunction_Check(func));
392     if (kwnames && PyTuple_GET_SIZE(kwnames)) {
393         PyObject *funcstr = _PyObject_FunctionStr(func);
394         if (funcstr != NULL) {
395             _PyErr_Format(tstate, PyExc_TypeError,
396                          "%U takes no keyword arguments", funcstr);
397             Py_DECREF(funcstr);
398         }
399         return -1;
400     }
401     return 0;
402 }
403 
404 typedef void (*funcptr)(void);
405 
406 static inline funcptr
cfunction_enter_call(PyThreadState * tstate,PyObject * func)407 cfunction_enter_call(PyThreadState *tstate, PyObject *func)
408 {
409     if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
410         return NULL;
411     }
412     return (funcptr)PyCFunction_GET_FUNCTION(func);
413 }
414 
415 /* Now the actual vectorcall functions */
416 static PyObject *
cfunction_vectorcall_FASTCALL(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)417 cfunction_vectorcall_FASTCALL(
418     PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
419 {
420     PyThreadState *tstate = _PyThreadState_GET();
421     if (cfunction_check_kwargs(tstate, func, kwnames)) {
422         return NULL;
423     }
424     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
425     _PyCFunctionFast meth = (_PyCFunctionFast)
426                             cfunction_enter_call(tstate, func);
427     if (meth == NULL) {
428         return NULL;
429     }
430     PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
431     _Py_LeaveRecursiveCall(tstate);
432     return result;
433 }
434 
435 static PyObject *
cfunction_vectorcall_FASTCALL_KEYWORDS(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)436 cfunction_vectorcall_FASTCALL_KEYWORDS(
437     PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
438 {
439     PyThreadState *tstate = _PyThreadState_GET();
440     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
441     _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
442                                         cfunction_enter_call(tstate, func);
443     if (meth == NULL) {
444         return NULL;
445     }
446     PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
447     _Py_LeaveRecursiveCall(tstate);
448     return result;
449 }
450 
451 static PyObject *
cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)452 cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD(
453     PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
454 {
455     PyThreadState *tstate = _PyThreadState_GET();
456     PyTypeObject *cls = PyCFunction_GET_CLASS(func);
457     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
458     PyCMethod meth = (PyCMethod)cfunction_enter_call(tstate, func);
459     if (meth == NULL) {
460         return NULL;
461     }
462     PyObject *result = meth(PyCFunction_GET_SELF(func), cls, args, nargs, kwnames);
463     _Py_LeaveRecursiveCall(tstate);
464     return result;
465 }
466 
467 static PyObject *
cfunction_vectorcall_NOARGS(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)468 cfunction_vectorcall_NOARGS(
469     PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
470 {
471     PyThreadState *tstate = _PyThreadState_GET();
472     if (cfunction_check_kwargs(tstate, func, kwnames)) {
473         return NULL;
474     }
475     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
476     if (nargs != 0) {
477         PyObject *funcstr = _PyObject_FunctionStr(func);
478         if (funcstr != NULL) {
479             _PyErr_Format(tstate, PyExc_TypeError,
480                 "%U takes no arguments (%zd given)", funcstr, nargs);
481             Py_DECREF(funcstr);
482         }
483         return NULL;
484     }
485     PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
486     if (meth == NULL) {
487         return NULL;
488     }
489     PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
490     _Py_LeaveRecursiveCall(tstate);
491     return result;
492 }
493 
494 static PyObject *
cfunction_vectorcall_O(PyObject * func,PyObject * const * args,size_t nargsf,PyObject * kwnames)495 cfunction_vectorcall_O(
496     PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
497 {
498     PyThreadState *tstate = _PyThreadState_GET();
499     if (cfunction_check_kwargs(tstate, func, kwnames)) {
500         return NULL;
501     }
502     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
503     if (nargs != 1) {
504         PyObject *funcstr = _PyObject_FunctionStr(func);
505         if (funcstr != NULL) {
506             _PyErr_Format(tstate, PyExc_TypeError,
507                 "%U takes exactly one argument (%zd given)", funcstr, nargs);
508             Py_DECREF(funcstr);
509         }
510         return NULL;
511     }
512     PyCFunction meth = (PyCFunction)cfunction_enter_call(tstate, func);
513     if (meth == NULL) {
514         return NULL;
515     }
516     PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
517     _Py_LeaveRecursiveCall(tstate);
518     return result;
519 }
520 
521 
522 static PyObject *
cfunction_call(PyObject * func,PyObject * args,PyObject * kwargs)523 cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
524 {
525     assert(kwargs == NULL || PyDict_Check(kwargs));
526 
527     PyThreadState *tstate = _PyThreadState_GET();
528     assert(!_PyErr_Occurred(tstate));
529 
530     int flags = PyCFunction_GET_FLAGS(func);
531     if (!(flags & METH_VARARGS)) {
532         /* If this is not a METH_VARARGS function, delegate to vectorcall */
533         return PyVectorcall_Call(func, args, kwargs);
534     }
535 
536     /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
537      * is NULL. This is intentional, since vectorcall would be slower. */
538     PyCFunction meth = PyCFunction_GET_FUNCTION(func);
539     PyObject *self = PyCFunction_GET_SELF(func);
540 
541     PyObject *result;
542     if (flags & METH_KEYWORDS) {
543         result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
544     }
545     else {
546         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
547             _PyErr_Format(tstate, PyExc_TypeError,
548                           "%.200s() takes no keyword arguments",
549                           ((PyCFunctionObject*)func)->m_ml->ml_name);
550             return NULL;
551         }
552         result = meth(self, args);
553     }
554     return _Py_CheckFunctionResult(tstate, func, result, NULL);
555 }
556