blob: 23325e2a1b3e2ae88269388506bf516f8ba28505 [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
Benjamin Petersone5024512018-09-12 12:06:42 -070022PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020023PyCFunction_New(PyMethodDef *ml, PyObject *self)
24{
25 return PyCFunction_NewEx(ml, self, NULL);
26}
27
28PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000029PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 PyCFunctionObject *op;
32 op = free_list;
33 if (op != NULL) {
34 free_list = (PyCFunctionObject *)(op->m_self);
Victor Stinnerb4435e22018-10-26 14:35:00 +020035 (void)PyObject_INIT((PyObject *)op, &PyCFunction_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 numfree--;
37 }
38 else {
39 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
40 if (op == NULL)
41 return NULL;
42 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040043 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 op->m_ml = ml;
45 Py_XINCREF(self);
46 op->m_self = self;
47 Py_XINCREF(module);
48 op->m_module = module;
49 _PyObject_GC_TRACK(op);
50 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051}
52
Guido van Rossumc0b618a1997-05-02 03:12:38 +000053PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000054PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 if (!PyCFunction_Check(op)) {
57 PyErr_BadInternalCall();
58 return NULL;
59 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010060 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061}
62
Guido van Rossumc0b618a1997-05-02 03:12:38 +000063PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000064PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (!PyCFunction_Check(op)) {
67 PyErr_BadInternalCall();
68 return NULL;
69 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010070 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
Guido van Rossumc0602291991-12-16 13:07:24 +000073int
Fred Drakeee238b92000-07-09 06:03:25 +000074PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (!PyCFunction_Check(op)) {
77 PyErr_BadInternalCall();
78 return -1;
79 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010080 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000081}
82
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083/* Methods (the standard built-in methods, that is) */
84
85static void
Fred Drakeee238b92000-07-09 06:03:25 +000086meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -040089 if (m->m_weakreflist != NULL) {
90 PyObject_ClearWeakRefs((PyObject*) m);
91 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 Py_XDECREF(m->m_self);
93 Py_XDECREF(m->m_module);
94 if (numfree < PyCFunction_MAXFREELIST) {
95 m->m_self = (PyObject *)free_list;
96 free_list = m;
97 numfree++;
98 }
99 else {
100 PyObject_GC_Del(m);
101 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800104static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530105meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored))
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800106{
107 PyObject *builtins;
108 PyObject *getattr;
109 _Py_IDENTIFIER(getattr);
110
111 if (m->m_self == NULL || PyModule_Check(m->m_self))
112 return PyUnicode_FromString(m->m_ml->ml_name);
113
114 builtins = PyEval_GetBuiltins();
115 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
116 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
117}
118
119static PyMethodDef meth_methods[] = {
120 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
121 {NULL, NULL}
122};
123
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800124static PyObject *
125meth_get__text_signature__(PyCFunctionObject *m, void *closure)
126{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800127 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800128}
129
Tim Peters6d6c1a32001-08-02 04:15:00 +0000130static PyObject *
131meth_get__doc__(PyCFunctionObject *m, void *closure)
132{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800133 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000134}
135
136static PyObject *
137meth_get__name__(PyCFunctionObject *m, void *closure)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000140}
141
Antoine Pitrou5b629422011-12-23 12:40:16 +0100142static PyObject *
143meth_get__qualname__(PyCFunctionObject *m, void *closure)
144{
145 /* If __self__ is a module or NULL, return m.__name__
146 (e.g. len.__qualname__ == 'len')
147
148 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
149 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
150
151 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
152 (e.g. [].append.__qualname__ == 'list.append') */
153 PyObject *type, *type_qualname, *res;
154 _Py_IDENTIFIER(__qualname__);
155
156 if (m->m_self == NULL || PyModule_Check(m->m_self))
157 return PyUnicode_FromString(m->m_ml->ml_name);
158
159 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
160
161 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
162 if (type_qualname == NULL)
163 return NULL;
164
165 if (!PyUnicode_Check(type_qualname)) {
166 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
167 "__qualname__ is not a unicode object");
168 Py_XDECREF(type_qualname);
169 return NULL;
170 }
171
172 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
173 Py_DECREF(type_qualname);
174 return res;
175}
176
Neil Schemenauer10c66922001-07-12 13:27:35 +0000177static int
178meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 Py_VISIT(m->m_self);
181 Py_VISIT(m->m_module);
182 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000183}
184
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000186meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000189
Antoine Pitrou5b629422011-12-23 12:40:16 +0100190 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (self == NULL)
192 self = Py_None;
193 Py_INCREF(self);
194 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000195}
196
Guido van Rossum32d34c82001-09-20 21:45:26 +0000197static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
199 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100200 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800202 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204};
205
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000206#define OFF(x) offsetof(PyCFunctionObject, x)
207
208static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
210 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000211};
212
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000214meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (m->m_self == NULL || PyModule_Check(m->m_self))
217 return PyUnicode_FromFormat("<built-in function %s>",
218 m->m_ml->ml_name);
219 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
220 m->m_ml->ml_name,
221 m->m_self->ob_type->tp_name,
222 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}
224
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000225static PyObject *
226meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 PyCFunctionObject *a, *b;
229 PyObject *res;
230 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if ((op != Py_EQ && op != Py_NE) ||
233 !PyCFunction_Check(self) ||
234 !PyCFunction_Check(other))
235 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500236 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 }
238 a = (PyCFunctionObject *)self;
239 b = (PyCFunctionObject *)other;
240 eq = a->m_self == b->m_self;
241 if (eq)
242 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
243 if (op == Py_EQ)
244 res = eq ? Py_True : Py_False;
245 else
246 res = eq ? Py_False : Py_True;
247 Py_INCREF(res);
248 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000249}
250
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000251static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000252meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000253{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000254 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300255 x = _Py_HashPointer(a->m_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 x ^= y;
258 if (x == -1)
259 x = -2;
260 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000261}
262
Tim Peters6d6c1a32001-08-02 04:15:00 +0000263
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyVarObject_HEAD_INIT(&PyType_Type, 0)
266 "builtin_function_or_method",
267 sizeof(PyCFunctionObject),
268 0,
269 (destructor)meth_dealloc, /* tp_dealloc */
270 0, /* tp_print */
271 0, /* tp_getattr */
272 0, /* tp_setattr */
273 0, /* tp_reserved */
274 (reprfunc)meth_repr, /* tp_repr */
275 0, /* tp_as_number */
276 0, /* tp_as_sequence */
277 0, /* tp_as_mapping */
278 (hashfunc)meth_hash, /* tp_hash */
279 PyCFunction_Call, /* tp_call */
280 0, /* tp_str */
281 PyObject_GenericGetAttr, /* tp_getattro */
282 0, /* tp_setattro */
283 0, /* tp_as_buffer */
284 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
285 0, /* tp_doc */
286 (traverseproc)meth_traverse, /* tp_traverse */
287 0, /* tp_clear */
288 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400289 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 0, /* tp_iter */
291 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800292 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 meth_members, /* tp_members */
294 meth_getsets, /* tp_getset */
295 0, /* tp_base */
296 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000299/* Clear out the free list */
300
Christian Heimesa156e092008-02-16 07:38:31 +0000301int
302PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 int freelist_size = numfree;
305
306 while (free_list) {
307 PyCFunctionObject *v = free_list;
308 free_list = (PyCFunctionObject *)(v->m_self);
309 PyObject_GC_Del(v);
310 numfree--;
311 }
312 assert(numfree == 0);
313 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000314}
315
316void
317PyCFunction_Fini(void)
318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000320}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000321
David Malcolm49526f42012-06-22 14:55:41 -0400322/* Print summary info about the state of the optimized allocator */
323void
324_PyCFunction_DebugMallocStats(FILE *out)
325{
326 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200327 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100328 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400329}