blob: 7d70cc04a238058c73562c82fc833dabc1b61531 [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
Andrew Svetlov4de29242012-12-26 23:08:54 +020010/* undefine macro trampoline to PyCFunction_NewEx */
11#undef PyCFunction_New
12
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020013/* Forward declarations */
14static PyObject * cfunction_vectorcall_FASTCALL(
15 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
16static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS(
17 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
18static PyObject * cfunction_vectorcall_NOARGS(
19 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
20static PyObject * cfunction_vectorcall_O(
21 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
22
23
Benjamin Petersone5024512018-09-12 12:06:42 -070024PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020025PyCFunction_New(PyMethodDef *ml, PyObject *self)
26{
27 return PyCFunction_NewEx(ml, self, NULL);
28}
29
30PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000031PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032{
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020033 /* Figure out correct vectorcall function to use */
34 vectorcallfunc vectorcall;
35 switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS))
36 {
37 case METH_VARARGS:
38 case METH_VARARGS | METH_KEYWORDS:
39 /* For METH_VARARGS functions, it's more efficient to use tp_call
40 * instead of vectorcall. */
41 vectorcall = NULL;
42 break;
43 case METH_FASTCALL:
44 vectorcall = cfunction_vectorcall_FASTCALL;
45 break;
46 case METH_FASTCALL | METH_KEYWORDS:
47 vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS;
48 break;
49 case METH_NOARGS:
50 vectorcall = cfunction_vectorcall_NOARGS;
51 break;
52 case METH_O:
53 vectorcall = cfunction_vectorcall_O;
54 break;
55 default:
56 PyErr_SetString(PyExc_SystemError, "bad call flags");
57 return NULL;
58 }
59
Inada Naoki3e54b572019-07-26 15:05:50 +090060 PyCFunctionObject *op =
61 PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
62 if (op == NULL) {
63 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040065 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 op->m_ml = ml;
67 Py_XINCREF(self);
68 op->m_self = self;
69 Py_XINCREF(module);
70 op->m_module = module;
Jeroen Demeyer0d722f32019-07-05 14:48:24 +020071 op->vectorcall = vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 _PyObject_GC_TRACK(op);
73 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000077PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (!PyCFunction_Check(op)) {
80 PyErr_BadInternalCall();
81 return NULL;
82 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010083 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084}
85
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000087PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 if (!PyCFunction_Check(op)) {
90 PyErr_BadInternalCall();
91 return NULL;
92 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010093 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094}
95
Guido van Rossumc0602291991-12-16 13:07:24 +000096int
Fred Drakeee238b92000-07-09 06:03:25 +000097PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (!PyCFunction_Check(op)) {
100 PyErr_BadInternalCall();
101 return -1;
102 }
Antoine Pitrou5b629422011-12-23 12:40:16 +0100103 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +0000104}
105
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106/* Methods (the standard built-in methods, that is) */
107
108static void
Fred Drakeee238b92000-07-09 06:03:25 +0000109meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400112 if (m->m_weakreflist != NULL) {
113 PyObject_ClearWeakRefs((PyObject*) m);
114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 Py_XDECREF(m->m_self);
116 Py_XDECREF(m->m_module);
Inada Naoki3e54b572019-07-26 15:05:50 +0900117 PyObject_GC_Del(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118}
119
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800120static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530121meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800122{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800123 _Py_IDENTIFIER(getattr);
124
125 if (m->m_self == NULL || PyModule_Check(m->m_self))
126 return PyUnicode_FromString(m->m_ml->ml_name);
127
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200128 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
129 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800130}
131
132static PyMethodDef meth_methods[] = {
133 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
134 {NULL, NULL}
135};
136
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800137static PyObject *
138meth_get__text_signature__(PyCFunctionObject *m, void *closure)
139{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800140 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800141}
142
Tim Peters6d6c1a32001-08-02 04:15:00 +0000143static PyObject *
144meth_get__doc__(PyCFunctionObject *m, void *closure)
145{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800146 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000147}
148
149static PyObject *
150meth_get__name__(PyCFunctionObject *m, void *closure)
151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000153}
154
Antoine Pitrou5b629422011-12-23 12:40:16 +0100155static PyObject *
156meth_get__qualname__(PyCFunctionObject *m, void *closure)
157{
158 /* If __self__ is a module or NULL, return m.__name__
159 (e.g. len.__qualname__ == 'len')
160
161 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
162 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
163
164 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
165 (e.g. [].append.__qualname__ == 'list.append') */
166 PyObject *type, *type_qualname, *res;
167 _Py_IDENTIFIER(__qualname__);
168
169 if (m->m_self == NULL || PyModule_Check(m->m_self))
170 return PyUnicode_FromString(m->m_ml->ml_name);
171
172 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
173
174 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
175 if (type_qualname == NULL)
176 return NULL;
177
178 if (!PyUnicode_Check(type_qualname)) {
179 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
180 "__qualname__ is not a unicode object");
181 Py_XDECREF(type_qualname);
182 return NULL;
183 }
184
185 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
186 Py_DECREF(type_qualname);
187 return res;
188}
189
Neil Schemenauer10c66922001-07-12 13:27:35 +0000190static int
191meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 Py_VISIT(m->m_self);
194 Py_VISIT(m->m_module);
195 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000196}
197
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000199meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000202
Antoine Pitrou5b629422011-12-23 12:40:16 +0100203 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (self == NULL)
205 self = Py_None;
206 Py_INCREF(self);
207 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000208}
209
Guido van Rossum32d34c82001-09-20 21:45:26 +0000210static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
212 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100213 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800215 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000217};
218
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000219#define OFF(x) offsetof(PyCFunctionObject, x)
220
221static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
223 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000224};
225
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000227meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (m->m_self == NULL || PyModule_Check(m->m_self))
230 return PyUnicode_FromFormat("<built-in function %s>",
231 m->m_ml->ml_name);
232 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
233 m->m_ml->ml_name,
234 m->m_self->ob_type->tp_name,
235 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000238static PyObject *
239meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyCFunctionObject *a, *b;
242 PyObject *res;
243 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if ((op != Py_EQ && op != Py_NE) ||
246 !PyCFunction_Check(self) ||
247 !PyCFunction_Check(other))
248 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500249 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 }
251 a = (PyCFunctionObject *)self;
252 b = (PyCFunctionObject *)other;
253 eq = a->m_self == b->m_self;
254 if (eq)
255 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
256 if (op == Py_EQ)
257 res = eq ? Py_True : Py_False;
258 else
259 res = eq ? Py_False : Py_True;
260 Py_INCREF(res);
261 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000262}
263
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000264static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000265meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000266{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000267 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300268 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 x ^= y;
271 if (x == -1)
272 x = -2;
273 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274}
275
Tim Peters6d6c1a32001-08-02 04:15:00 +0000276
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 PyVarObject_HEAD_INIT(&PyType_Type, 0)
279 "builtin_function_or_method",
280 sizeof(PyCFunctionObject),
281 0,
282 (destructor)meth_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200283 offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 0, /* tp_getattr */
285 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200286 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 (reprfunc)meth_repr, /* tp_repr */
288 0, /* tp_as_number */
289 0, /* tp_as_sequence */
290 0, /* tp_as_mapping */
291 (hashfunc)meth_hash, /* tp_hash */
292 PyCFunction_Call, /* tp_call */
293 0, /* tp_str */
294 PyObject_GenericGetAttr, /* tp_getattro */
295 0, /* tp_setattro */
296 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200297 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
298 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 0, /* tp_doc */
300 (traverseproc)meth_traverse, /* tp_traverse */
301 0, /* tp_clear */
302 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400303 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 0, /* tp_iter */
305 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800306 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 meth_members, /* tp_members */
308 meth_getsets, /* tp_getset */
309 0, /* tp_base */
310 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000312
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000313/* Clear out the free list */
314
Christian Heimesa156e092008-02-16 07:38:31 +0000315int
316PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000317{
Inada Naoki3e54b572019-07-26 15:05:50 +0900318 return 0;
Christian Heimesa156e092008-02-16 07:38:31 +0000319}
320
321void
Victor Stinnerbed48172019-08-27 00:12:32 +0200322_PyCFunction_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000325}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000326
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200327
328/* Vectorcall functions for each of the PyCFunction calling conventions,
329 * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which
330 * doesn't use vectorcall.
331 *
332 * First, common helpers
333 */
334static const char *
335get_name(PyObject *func)
336{
337 assert(PyCFunction_Check(func));
338 PyMethodDef *method = ((PyCFunctionObject *)func)->m_ml;
339 return method->ml_name;
340}
341
342typedef void (*funcptr)(void);
343
344static inline int
345cfunction_check_kwargs(PyObject *func, PyObject *kwnames)
346{
347 assert(!PyErr_Occurred());
348 assert(PyCFunction_Check(func));
349 if (kwnames && PyTuple_GET_SIZE(kwnames)) {
350 PyErr_Format(PyExc_TypeError,
351 "%.200s() takes no keyword arguments", get_name(func));
352 return -1;
353 }
354 return 0;
355}
356
357static inline funcptr
358cfunction_enter_call(PyObject *func)
359{
360 if (Py_EnterRecursiveCall(" while calling a Python object")) {
361 return NULL;
362 }
363 return (funcptr)PyCFunction_GET_FUNCTION(func);
364}
365
366/* Now the actual vectorcall functions */
367static PyObject *
368cfunction_vectorcall_FASTCALL(
369 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
370{
371 if (cfunction_check_kwargs(func, kwnames)) {
372 return NULL;
373 }
374 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
375 _PyCFunctionFast meth = (_PyCFunctionFast)
376 cfunction_enter_call(func);
377 if (meth == NULL) {
378 return NULL;
379 }
380 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs);
381 Py_LeaveRecursiveCall();
382 return result;
383}
384
385static PyObject *
386cfunction_vectorcall_FASTCALL_KEYWORDS(
387 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
388{
389 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
390 _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
391 cfunction_enter_call(func);
392 if (meth == NULL) {
393 return NULL;
394 }
395 PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames);
396 Py_LeaveRecursiveCall();
397 return result;
398}
399
400static PyObject *
401cfunction_vectorcall_NOARGS(
402 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
403{
404 if (cfunction_check_kwargs(func, kwnames)) {
405 return NULL;
406 }
407 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
408 if (nargs != 0) {
409 PyErr_Format(PyExc_TypeError,
410 "%.200s() takes no arguments (%zd given)", get_name(func), nargs);
411 return NULL;
412 }
413 PyCFunction meth = (PyCFunction)cfunction_enter_call(func);
414 if (meth == NULL) {
415 return NULL;
416 }
417 PyObject *result = meth(PyCFunction_GET_SELF(func), NULL);
418 Py_LeaveRecursiveCall();
419 return result;
420}
421
422static PyObject *
423cfunction_vectorcall_O(
424 PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
425{
426 if (cfunction_check_kwargs(func, kwnames)) {
427 return NULL;
428 }
429 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
430 if (nargs != 1) {
431 PyErr_Format(PyExc_TypeError,
432 "%.200s() takes exactly one argument (%zd given)",
433 get_name(func), nargs);
434 return NULL;
435 }
436 PyCFunction meth = (PyCFunction)cfunction_enter_call(func);
437 if (meth == NULL) {
438 return NULL;
439 }
440 PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]);
441 Py_LeaveRecursiveCall();
442 return result;
443}