blob: 4050922e03c1de4a1167928f4a4827ca3aaec145 [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"
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Christian Heimes2202f872008-02-06 14:31:34 +00007/* Free list for method objects to safe malloc/free overhead
8 * The m_self element is used to chain the objects.
9 */
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000010static PyCFunctionObject *free_list = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +000011static int numfree = 0;
12#ifndef PyCFunction_MAXFREELIST
13#define PyCFunction_MAXFREELIST 256
14#endif
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000015
Andrew Svetlov4de29242012-12-26 23:08:54 +020016/* undefine macro trampoline to PyCFunction_NewEx */
17#undef PyCFunction_New
18
Andrew Svetlov9df36c92015-04-27 17:48:50 +030019PyAPI_FUNC(PyObject *)
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020020PyCFunction_New(PyMethodDef *ml, PyObject *self)
21{
22 return PyCFunction_NewEx(ml, self, NULL);
23}
24
25PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000026PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 PyCFunctionObject *op;
29 op = free_list;
30 if (op != NULL) {
31 free_list = (PyCFunctionObject *)(op->m_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010032 (void)PyObject_INIT(op, &PyCFunction_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 numfree--;
34 }
35 else {
36 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
37 if (op == NULL)
38 return NULL;
39 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040040 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 op->m_ml = ml;
42 Py_XINCREF(self);
43 op->m_self = self;
44 Py_XINCREF(module);
45 op->m_module = module;
46 _PyObject_GC_TRACK(op);
47 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048}
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000051PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 if (!PyCFunction_Check(op)) {
54 PyErr_BadInternalCall();
55 return NULL;
56 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010057 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000061PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 if (!PyCFunction_Check(op)) {
64 PyErr_BadInternalCall();
65 return NULL;
66 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010067 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossumc0602291991-12-16 13:07:24 +000070int
Fred Drakeee238b92000-07-09 06:03:25 +000071PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (!PyCFunction_Check(op)) {
74 PyErr_BadInternalCall();
75 return -1;
76 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010077 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000078}
79
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080/* Methods (the standard built-in methods, that is) */
81
82static void
Fred Drakeee238b92000-07-09 06:03:25 +000083meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -040086 if (m->m_weakreflist != NULL) {
87 PyObject_ClearWeakRefs((PyObject*) m);
88 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 Py_XDECREF(m->m_self);
90 Py_XDECREF(m->m_module);
91 if (numfree < PyCFunction_MAXFREELIST) {
92 m->m_self = (PyObject *)free_list;
93 free_list = m;
94 numfree++;
95 }
96 else {
97 PyObject_GC_Del(m);
98 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800101static PyObject *
102meth_reduce(PyCFunctionObject *m)
103{
104 PyObject *builtins;
105 PyObject *getattr;
106 _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
111 builtins = PyEval_GetBuiltins();
112 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
113 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
114}
115
116static PyMethodDef meth_methods[] = {
117 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
118 {NULL, NULL}
119};
120
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800121static PyObject *
122meth_get__text_signature__(PyCFunctionObject *m, void *closure)
123{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800124 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800125}
126
Tim Peters6d6c1a32001-08-02 04:15:00 +0000127static PyObject *
128meth_get__doc__(PyCFunctionObject *m, void *closure)
129{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800130 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000131}
132
133static PyObject *
134meth_get__name__(PyCFunctionObject *m, void *closure)
135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000137}
138
Antoine Pitrou5b629422011-12-23 12:40:16 +0100139static PyObject *
140meth_get__qualname__(PyCFunctionObject *m, void *closure)
141{
142 /* If __self__ is a module or NULL, return m.__name__
143 (e.g. len.__qualname__ == 'len')
144
145 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
146 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
147
148 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
149 (e.g. [].append.__qualname__ == 'list.append') */
150 PyObject *type, *type_qualname, *res;
151 _Py_IDENTIFIER(__qualname__);
152
153 if (m->m_self == NULL || PyModule_Check(m->m_self))
154 return PyUnicode_FromString(m->m_ml->ml_name);
155
156 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
157
158 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
159 if (type_qualname == NULL)
160 return NULL;
161
162 if (!PyUnicode_Check(type_qualname)) {
163 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
164 "__qualname__ is not a unicode object");
165 Py_XDECREF(type_qualname);
166 return NULL;
167 }
168
169 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
170 Py_DECREF(type_qualname);
171 return res;
172}
173
Neil Schemenauer10c66922001-07-12 13:27:35 +0000174static int
175meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 Py_VISIT(m->m_self);
178 Py_VISIT(m->m_module);
179 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000180}
181
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000186
Antoine Pitrou5b629422011-12-23 12:40:16 +0100187 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (self == NULL)
189 self = Py_None;
190 Py_INCREF(self);
191 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000192}
193
Guido van Rossum32d34c82001-09-20 21:45:26 +0000194static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
196 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100197 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800199 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000201};
202
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000203#define OFF(x) offsetof(PyCFunctionObject, x)
204
205static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
207 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000208};
209
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000211meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (m->m_self == NULL || PyModule_Check(m->m_self))
214 return PyUnicode_FromFormat("<built-in function %s>",
215 m->m_ml->ml_name);
216 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
217 m->m_ml->ml_name,
218 m->m_self->ob_type->tp_name,
219 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220}
221
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000222static PyObject *
223meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 PyCFunctionObject *a, *b;
226 PyObject *res;
227 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if ((op != Py_EQ && op != Py_NE) ||
230 !PyCFunction_Check(self) ||
231 !PyCFunction_Check(other))
232 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500233 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 }
235 a = (PyCFunctionObject *)self;
236 b = (PyCFunctionObject *)other;
237 eq = a->m_self == b->m_self;
238 if (eq)
239 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
240 if (op == Py_EQ)
241 res = eq ? Py_True : Py_False;
242 else
243 res = eq ? Py_False : Py_True;
244 Py_INCREF(res);
245 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000246}
247
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000248static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000249meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000250{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000251 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (a->m_self == NULL)
253 x = 0;
254 else {
255 x = PyObject_Hash(a->m_self);
256 if (x == -1)
257 return -1;
258 }
259 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
260 if (y == -1)
261 return -1;
262 x ^= y;
263 if (x == -1)
264 x = -2;
265 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000266}
267
Tim Peters6d6c1a32001-08-02 04:15:00 +0000268
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 PyVarObject_HEAD_INIT(&PyType_Type, 0)
271 "builtin_function_or_method",
272 sizeof(PyCFunctionObject),
273 0,
274 (destructor)meth_dealloc, /* tp_dealloc */
275 0, /* tp_print */
276 0, /* tp_getattr */
277 0, /* tp_setattr */
278 0, /* tp_reserved */
279 (reprfunc)meth_repr, /* tp_repr */
280 0, /* tp_as_number */
281 0, /* tp_as_sequence */
282 0, /* tp_as_mapping */
283 (hashfunc)meth_hash, /* tp_hash */
284 PyCFunction_Call, /* tp_call */
285 0, /* tp_str */
286 PyObject_GenericGetAttr, /* tp_getattro */
287 0, /* tp_setattro */
288 0, /* tp_as_buffer */
289 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
290 0, /* tp_doc */
291 (traverseproc)meth_traverse, /* tp_traverse */
292 0, /* tp_clear */
293 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400294 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 0, /* tp_iter */
296 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800297 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 meth_members, /* tp_members */
299 meth_getsets, /* tp_getset */
300 0, /* tp_base */
301 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000303
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000304/* Clear out the free list */
305
Christian Heimesa156e092008-02-16 07:38:31 +0000306int
307PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 int freelist_size = numfree;
310
311 while (free_list) {
312 PyCFunctionObject *v = free_list;
313 free_list = (PyCFunctionObject *)(v->m_self);
314 PyObject_GC_Del(v);
315 numfree--;
316 }
317 assert(numfree == 0);
318 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000319}
320
321void
322PyCFunction_Fini(void)
323{
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
David Malcolm49526f42012-06-22 14:55:41 -0400327/* Print summary info about the state of the optimized allocator */
328void
329_PyCFunction_DebugMallocStats(FILE *out)
330{
331 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200332 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100333 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400334}