blob: f9bac1922dc793c1416ed6463b907b42619dad4f [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/mem.h"
6#include "internal/pystate.h"
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +00007#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Christian Heimes2202f872008-02-06 14:31:34 +00009/* Free list for method objects to safe malloc/free overhead
10 * The m_self element is used to chain the objects.
11 */
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000012static PyCFunctionObject *free_list = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +000013static int numfree = 0;
14#ifndef PyCFunction_MAXFREELIST
15#define PyCFunction_MAXFREELIST 256
16#endif
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000017
Andrew Svetlov4de29242012-12-26 23:08:54 +020018/* undefine macro trampoline to PyCFunction_NewEx */
19#undef PyCFunction_New
20
Andrew Svetlov9df36c92015-04-27 17:48:50 +030021PyAPI_FUNC(PyObject *)
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020022PyCFunction_New(PyMethodDef *ml, PyObject *self)
23{
24 return PyCFunction_NewEx(ml, self, NULL);
25}
26
27PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000028PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyCFunctionObject *op;
31 op = free_list;
32 if (op != NULL) {
33 free_list = (PyCFunctionObject *)(op->m_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010034 (void)PyObject_INIT(op, &PyCFunction_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 numfree--;
36 }
37 else {
38 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
39 if (op == NULL)
40 return NULL;
41 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040042 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 op->m_ml = ml;
44 Py_XINCREF(self);
45 op->m_self = self;
46 Py_XINCREF(module);
47 op->m_module = module;
48 _PyObject_GC_TRACK(op);
49 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050}
51
Guido van Rossumc0b618a1997-05-02 03:12:38 +000052PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000053PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 if (!PyCFunction_Check(op)) {
56 PyErr_BadInternalCall();
57 return NULL;
58 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010059 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060}
61
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000063PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (!PyCFunction_Check(op)) {
66 PyErr_BadInternalCall();
67 return NULL;
68 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010069 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Guido van Rossumc0602291991-12-16 13:07:24 +000072int
Fred Drakeee238b92000-07-09 06:03:25 +000073PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (!PyCFunction_Check(op)) {
76 PyErr_BadInternalCall();
77 return -1;
78 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010079 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000080}
81
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082/* Methods (the standard built-in methods, that is) */
83
84static void
Fred Drakeee238b92000-07-09 06:03:25 +000085meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -040088 if (m->m_weakreflist != NULL) {
89 PyObject_ClearWeakRefs((PyObject*) m);
90 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 Py_XDECREF(m->m_self);
92 Py_XDECREF(m->m_module);
93 if (numfree < PyCFunction_MAXFREELIST) {
94 m->m_self = (PyObject *)free_list;
95 free_list = m;
96 numfree++;
97 }
98 else {
99 PyObject_GC_Del(m);
100 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101}
102
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800103static PyObject *
104meth_reduce(PyCFunctionObject *m)
105{
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800106 _Py_IDENTIFIER(getattr);
107
108 if (m->m_self == NULL || PyModule_Check(m->m_self))
109 return PyUnicode_FromString(m->m_ml->ml_name);
110
Serhiy Storchaka3cae16d2018-12-11 10:51:27 +0200111 return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
112 m->m_self, m->m_ml->ml_name);
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800113}
114
115static PyMethodDef meth_methods[] = {
116 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
117 {NULL, NULL}
118};
119
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800120static PyObject *
121meth_get__text_signature__(PyCFunctionObject *m, void *closure)
122{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800123 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800124}
125
Tim Peters6d6c1a32001-08-02 04:15:00 +0000126static PyObject *
127meth_get__doc__(PyCFunctionObject *m, void *closure)
128{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800129 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000130}
131
132static PyObject *
133meth_get__name__(PyCFunctionObject *m, void *closure)
134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136}
137
Antoine Pitrou5b629422011-12-23 12:40:16 +0100138static PyObject *
139meth_get__qualname__(PyCFunctionObject *m, void *closure)
140{
141 /* If __self__ is a module or NULL, return m.__name__
142 (e.g. len.__qualname__ == 'len')
143
144 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
145 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
146
147 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
148 (e.g. [].append.__qualname__ == 'list.append') */
149 PyObject *type, *type_qualname, *res;
150 _Py_IDENTIFIER(__qualname__);
151
152 if (m->m_self == NULL || PyModule_Check(m->m_self))
153 return PyUnicode_FromString(m->m_ml->ml_name);
154
155 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
156
157 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
158 if (type_qualname == NULL)
159 return NULL;
160
161 if (!PyUnicode_Check(type_qualname)) {
162 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
163 "__qualname__ is not a unicode object");
164 Py_XDECREF(type_qualname);
165 return NULL;
166 }
167
168 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
169 Py_DECREF(type_qualname);
170 return res;
171}
172
Neil Schemenauer10c66922001-07-12 13:27:35 +0000173static int
174meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 Py_VISIT(m->m_self);
177 Py_VISIT(m->m_module);
178 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000179}
180
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000185
Antoine Pitrou5b629422011-12-23 12:40:16 +0100186 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (self == NULL)
188 self = Py_None;
189 Py_INCREF(self);
190 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000191}
192
Guido van Rossum32d34c82001-09-20 21:45:26 +0000193static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
195 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100196 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800198 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200};
201
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000202#define OFF(x) offsetof(PyCFunctionObject, x)
203
204static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
206 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000207};
208
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000210meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (m->m_self == NULL || PyModule_Check(m->m_self))
213 return PyUnicode_FromFormat("<built-in function %s>",
214 m->m_ml->ml_name);
215 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
216 m->m_ml->ml_name,
217 m->m_self->ob_type->tp_name,
218 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219}
220
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000221static PyObject *
222meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyCFunctionObject *a, *b;
225 PyObject *res;
226 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if ((op != Py_EQ && op != Py_NE) ||
229 !PyCFunction_Check(self) ||
230 !PyCFunction_Check(other))
231 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500232 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
234 a = (PyCFunctionObject *)self;
235 b = (PyCFunctionObject *)other;
236 eq = a->m_self == b->m_self;
237 if (eq)
238 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
239 if (op == Py_EQ)
240 res = eq ? Py_True : Py_False;
241 else
242 res = eq ? Py_False : Py_True;
243 Py_INCREF(res);
244 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000245}
246
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000247static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000248meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000249{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000250 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (a->m_self == NULL)
252 x = 0;
253 else {
254 x = PyObject_Hash(a->m_self);
255 if (x == -1)
256 return -1;
257 }
258 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
259 if (y == -1)
260 return -1;
261 x ^= y;
262 if (x == -1)
263 x = -2;
264 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000265}
266
Tim Peters6d6c1a32001-08-02 04:15:00 +0000267
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000268PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyVarObject_HEAD_INIT(&PyType_Type, 0)
270 "builtin_function_or_method",
271 sizeof(PyCFunctionObject),
272 0,
273 (destructor)meth_dealloc, /* tp_dealloc */
274 0, /* tp_print */
275 0, /* tp_getattr */
276 0, /* tp_setattr */
277 0, /* tp_reserved */
278 (reprfunc)meth_repr, /* tp_repr */
279 0, /* tp_as_number */
280 0, /* tp_as_sequence */
281 0, /* tp_as_mapping */
282 (hashfunc)meth_hash, /* tp_hash */
283 PyCFunction_Call, /* tp_call */
284 0, /* tp_str */
285 PyObject_GenericGetAttr, /* tp_getattro */
286 0, /* tp_setattro */
287 0, /* tp_as_buffer */
288 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
289 0, /* tp_doc */
290 (traverseproc)meth_traverse, /* tp_traverse */
291 0, /* tp_clear */
292 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400293 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 0, /* tp_iter */
295 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800296 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 meth_members, /* tp_members */
298 meth_getsets, /* tp_getset */
299 0, /* tp_base */
300 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000302
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000303/* Clear out the free list */
304
Christian Heimesa156e092008-02-16 07:38:31 +0000305int
306PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 int freelist_size = numfree;
309
310 while (free_list) {
311 PyCFunctionObject *v = free_list;
312 free_list = (PyCFunctionObject *)(v->m_self);
313 PyObject_GC_Del(v);
314 numfree--;
315 }
316 assert(numfree == 0);
317 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000318}
319
320void
321PyCFunction_Fini(void)
322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000324}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000325
David Malcolm49526f42012-06-22 14:55:41 -0400326/* Print summary info about the state of the optimized allocator */
327void
328_PyCFunction_DebugMallocStats(FILE *out)
329{
330 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200331 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100332 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400333}