blob: 5d9f36403424ee4a07effe1ed8d62f2b86db0782 [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016PyObject *
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020017PyCFunction_New(PyMethodDef *ml, PyObject *self)
18{
19 return PyCFunction_NewEx(ml, self, NULL);
20}
21
22PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000023PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 PyCFunctionObject *op;
26 op = free_list;
27 if (op != NULL) {
28 free_list = (PyCFunctionObject *)(op->m_self);
29 PyObject_INIT(op, &PyCFunction_Type);
30 numfree--;
31 }
32 else {
33 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
34 if (op == NULL)
35 return NULL;
36 }
37 op->m_ml = ml;
38 Py_XINCREF(self);
39 op->m_self = self;
40 Py_XINCREF(module);
41 op->m_module = module;
42 _PyObject_GC_TRACK(op);
43 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044}
45
Guido van Rossumc0b618a1997-05-02 03:12:38 +000046PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000047PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (!PyCFunction_Check(op)) {
50 PyErr_BadInternalCall();
51 return NULL;
52 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010053 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054}
55
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000057PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (!PyCFunction_Check(op)) {
60 PyErr_BadInternalCall();
61 return NULL;
62 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010063 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064}
65
Guido van Rossumc0602291991-12-16 13:07:24 +000066int
Fred Drakeee238b92000-07-09 06:03:25 +000067PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (!PyCFunction_Check(op)) {
70 PyErr_BadInternalCall();
71 return -1;
72 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010073 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000074}
75
Jeremy Hylton910d7d42001-08-12 21:52:24 +000076PyObject *
77PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
78{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 PyCFunctionObject* f = (PyCFunctionObject*)func;
80 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
81 PyObject *self = PyCFunction_GET_SELF(func);
82 Py_ssize_t size;
Jeremy Hylton910d7d42001-08-12 21:52:24 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
85 case METH_VARARGS:
86 if (kw == NULL || PyDict_Size(kw) == 0)
87 return (*meth)(self, arg);
88 break;
89 case METH_VARARGS | METH_KEYWORDS:
90 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
91 case METH_NOARGS:
92 if (kw == NULL || PyDict_Size(kw) == 0) {
93 size = PyTuple_GET_SIZE(arg);
94 if (size == 0)
95 return (*meth)(self, NULL);
96 PyErr_Format(PyExc_TypeError,
97 "%.200s() takes no arguments (%zd given)",
98 f->m_ml->ml_name, size);
99 return NULL;
100 }
101 break;
102 case METH_O:
103 if (kw == NULL || PyDict_Size(kw) == 0) {
104 size = PyTuple_GET_SIZE(arg);
105 if (size == 1)
106 return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
107 PyErr_Format(PyExc_TypeError,
108 "%.200s() takes exactly one argument (%zd given)",
109 f->m_ml->ml_name, size);
110 return NULL;
111 }
112 break;
113 default:
114 PyErr_SetString(PyExc_SystemError, "Bad call flags in "
115 "PyCFunction_Call. METH_OLDARGS is no "
116 "longer supported!");
117
118 return NULL;
119 }
120 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
121 f->m_ml->ml_name);
122 return NULL;
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000123}
124
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125/* Methods (the standard built-in methods, that is) */
126
127static void
Fred Drakeee238b92000-07-09 06:03:25 +0000128meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 _PyObject_GC_UNTRACK(m);
131 Py_XDECREF(m->m_self);
132 Py_XDECREF(m->m_module);
133 if (numfree < PyCFunction_MAXFREELIST) {
134 m->m_self = (PyObject *)free_list;
135 free_list = m;
136 numfree++;
137 }
138 else {
139 PyObject_GC_Del(m);
140 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
Tim Peters6d6c1a32001-08-02 04:15:00 +0000143static PyObject *
144meth_get__doc__(PyCFunctionObject *m, void *closure)
145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 const char *doc = m->m_ml->ml_doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (doc != NULL)
149 return PyUnicode_FromString(doc);
150 Py_INCREF(Py_None);
151 return Py_None;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152}
153
154static PyObject *
155meth_get__name__(PyCFunctionObject *m, void *closure)
156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000158}
159
Antoine Pitrou5b629422011-12-23 12:40:16 +0100160static PyObject *
161meth_get__qualname__(PyCFunctionObject *m, void *closure)
162{
163 /* If __self__ is a module or NULL, return m.__name__
164 (e.g. len.__qualname__ == 'len')
165
166 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
167 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
168
169 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
170 (e.g. [].append.__qualname__ == 'list.append') */
171 PyObject *type, *type_qualname, *res;
172 _Py_IDENTIFIER(__qualname__);
173
174 if (m->m_self == NULL || PyModule_Check(m->m_self))
175 return PyUnicode_FromString(m->m_ml->ml_name);
176
177 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
178
179 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
180 if (type_qualname == NULL)
181 return NULL;
182
183 if (!PyUnicode_Check(type_qualname)) {
184 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
185 "__qualname__ is not a unicode object");
186 Py_XDECREF(type_qualname);
187 return NULL;
188 }
189
190 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
191 Py_DECREF(type_qualname);
192 return res;
193}
194
Neil Schemenauer10c66922001-07-12 13:27:35 +0000195static int
196meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 Py_VISIT(m->m_self);
199 Py_VISIT(m->m_module);
200 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000201}
202
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000207
Antoine Pitrou5b629422011-12-23 12:40:16 +0100208 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (self == NULL)
210 self = Py_None;
211 Py_INCREF(self);
212 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000213}
214
Guido van Rossum32d34c82001-09-20 21:45:26 +0000215static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
217 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100218 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 {"__self__", (getter)meth_get__self__, NULL, NULL},
220 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000221};
222
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000223#define OFF(x) offsetof(PyCFunctionObject, x)
224
225static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
227 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000228};
229
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000231meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (m->m_self == NULL || PyModule_Check(m->m_self))
234 return PyUnicode_FromFormat("<built-in function %s>",
235 m->m_ml->ml_name);
236 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
237 m->m_ml->ml_name,
238 m->m_self->ob_type->tp_name,
239 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240}
241
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000242static PyObject *
243meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyCFunctionObject *a, *b;
246 PyObject *res;
247 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if ((op != Py_EQ && op != Py_NE) ||
250 !PyCFunction_Check(self) ||
251 !PyCFunction_Check(other))
252 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500253 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 }
255 a = (PyCFunctionObject *)self;
256 b = (PyCFunctionObject *)other;
257 eq = a->m_self == b->m_self;
258 if (eq)
259 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
260 if (op == Py_EQ)
261 res = eq ? Py_True : Py_False;
262 else
263 res = eq ? Py_False : Py_True;
264 Py_INCREF(res);
265 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000266}
267
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000268static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000269meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000271 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (a->m_self == NULL)
273 x = 0;
274 else {
275 x = PyObject_Hash(a->m_self);
276 if (x == -1)
277 return -1;
278 }
279 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
280 if (y == -1)
281 return -1;
282 x ^= y;
283 if (x == -1)
284 x = -2;
285 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000286}
287
Tim Peters6d6c1a32001-08-02 04:15:00 +0000288
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyVarObject_HEAD_INIT(&PyType_Type, 0)
291 "builtin_function_or_method",
292 sizeof(PyCFunctionObject),
293 0,
294 (destructor)meth_dealloc, /* tp_dealloc */
295 0, /* tp_print */
296 0, /* tp_getattr */
297 0, /* tp_setattr */
298 0, /* tp_reserved */
299 (reprfunc)meth_repr, /* tp_repr */
300 0, /* tp_as_number */
301 0, /* tp_as_sequence */
302 0, /* tp_as_mapping */
303 (hashfunc)meth_hash, /* tp_hash */
304 PyCFunction_Call, /* tp_call */
305 0, /* tp_str */
306 PyObject_GenericGetAttr, /* tp_getattro */
307 0, /* tp_setattro */
308 0, /* tp_as_buffer */
309 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
310 0, /* tp_doc */
311 (traverseproc)meth_traverse, /* tp_traverse */
312 0, /* tp_clear */
313 meth_richcompare, /* tp_richcompare */
314 0, /* tp_weaklistoffset */
315 0, /* tp_iter */
316 0, /* tp_iternext */
317 0, /* tp_methods */
318 meth_members, /* tp_members */
319 meth_getsets, /* tp_getset */
320 0, /* tp_base */
321 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000323
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000324/* Clear out the free list */
325
Christian Heimesa156e092008-02-16 07:38:31 +0000326int
327PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 int freelist_size = numfree;
330
331 while (free_list) {
332 PyCFunctionObject *v = free_list;
333 free_list = (PyCFunctionObject *)(v->m_self);
334 PyObject_GC_Del(v);
335 numfree--;
336 }
337 assert(numfree == 0);
338 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000339}
340
341void
342PyCFunction_Fini(void)
343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000345}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000346
David Malcolm49526f42012-06-22 14:55:41 -0400347/* Print summary info about the state of the optimized allocator */
348void
349_PyCFunction_DebugMallocStats(FILE *out)
350{
351 _PyDebugAllocatorStats(out,
352 "free PyCFunction",
353 numfree, sizeof(PyCFunction));
354}