blob: 6179aeebd0ca627e2cf3e85799ea601571b71f35 [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +000019PyObject *
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 }
40 op->m_ml = ml;
41 Py_XINCREF(self);
42 op->m_self = self;
43 Py_XINCREF(module);
44 op->m_module = module;
45 _PyObject_GC_TRACK(op);
46 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047}
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000050PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (!PyCFunction_Check(op)) {
53 PyErr_BadInternalCall();
54 return NULL;
55 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010056 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000060PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 if (!PyCFunction_Check(op)) {
63 PyErr_BadInternalCall();
64 return NULL;
65 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010066 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossumc0602291991-12-16 13:07:24 +000069int
Fred Drakeee238b92000-07-09 06:03:25 +000070PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (!PyCFunction_Check(op)) {
73 PyErr_BadInternalCall();
74 return -1;
75 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010076 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000077}
78
Jeremy Hylton910d7d42001-08-12 21:52:24 +000079PyObject *
80PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
81{
Victor Stinner9035ad92013-07-11 23:44:46 +020082#define CHECK_RESULT(res) assert(res != NULL || PyErr_Occurred())
83
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 PyCFunctionObject* f = (PyCFunctionObject*)func;
85 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
86 PyObject *self = PyCFunction_GET_SELF(func);
Victor Stinner9035ad92013-07-11 23:44:46 +020087 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 Py_ssize_t size;
Jeremy Hylton910d7d42001-08-12 21:52:24 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
91 case METH_VARARGS:
Victor Stinner9035ad92013-07-11 23:44:46 +020092 if (kw == NULL || PyDict_Size(kw) == 0) {
93 res = (*meth)(self, arg);
94 CHECK_RESULT(res);
95 return res;
96 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 break;
98 case METH_VARARGS | METH_KEYWORDS:
Victor Stinner9035ad92013-07-11 23:44:46 +020099 res = (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
100 CHECK_RESULT(res);
101 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 case METH_NOARGS:
103 if (kw == NULL || PyDict_Size(kw) == 0) {
104 size = PyTuple_GET_SIZE(arg);
Victor Stinner9035ad92013-07-11 23:44:46 +0200105 if (size == 0) {
106 res = (*meth)(self, NULL);
107 CHECK_RESULT(res);
108 return res;
109 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyErr_Format(PyExc_TypeError,
111 "%.200s() takes no arguments (%zd given)",
112 f->m_ml->ml_name, size);
113 return NULL;
114 }
115 break;
116 case METH_O:
117 if (kw == NULL || PyDict_Size(kw) == 0) {
118 size = PyTuple_GET_SIZE(arg);
Victor Stinner9035ad92013-07-11 23:44:46 +0200119 if (size == 1) {
120 res = (*meth)(self, PyTuple_GET_ITEM(arg, 0));
121 CHECK_RESULT(res);
122 return res;
123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyErr_Format(PyExc_TypeError,
125 "%.200s() takes exactly one argument (%zd given)",
126 f->m_ml->ml_name, size);
127 return NULL;
128 }
129 break;
130 default:
131 PyErr_SetString(PyExc_SystemError, "Bad call flags in "
132 "PyCFunction_Call. METH_OLDARGS is no "
133 "longer supported!");
134
135 return NULL;
136 }
137 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
138 f->m_ml->ml_name);
139 return NULL;
Victor Stinner9035ad92013-07-11 23:44:46 +0200140
141#undef CHECK_RESULT
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000142}
143
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144/* Methods (the standard built-in methods, that is) */
145
146static void
Fred Drakeee238b92000-07-09 06:03:25 +0000147meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 _PyObject_GC_UNTRACK(m);
150 Py_XDECREF(m->m_self);
151 Py_XDECREF(m->m_module);
152 if (numfree < PyCFunction_MAXFREELIST) {
153 m->m_self = (PyObject *)free_list;
154 free_list = m;
155 numfree++;
156 }
157 else {
158 PyObject_GC_Del(m);
159 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800162static PyObject *
163meth_reduce(PyCFunctionObject *m)
164{
165 PyObject *builtins;
166 PyObject *getattr;
167 _Py_IDENTIFIER(getattr);
168
169 if (m->m_self == NULL || PyModule_Check(m->m_self))
170 return PyUnicode_FromString(m->m_ml->ml_name);
171
172 builtins = PyEval_GetBuiltins();
173 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
174 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
175}
176
177static PyMethodDef meth_methods[] = {
178 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
179 {NULL, NULL}
180};
181
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800182/*
183 * finds the docstring's introspection signature.
184 * if present, returns a pointer pointing to the first '('.
185 * otherwise returns NULL.
186 */
187static const char *find_signature(PyCFunctionObject *m)
188{
189 const char *trace = m->m_ml->ml_doc;
190 const char *name = m->m_ml->ml_name;
191 size_t length;
192 if (!trace || !name)
193 return NULL;
194 length = strlen(name);
195 if (strncmp(trace, name, length))
196 return NULL;
197 trace += length;
198 if (*trace != '(')
199 return NULL;
200 return trace;
201}
202
203/*
204 * skips to the end of the docstring's instrospection signature.
205 */
206static const char *skip_signature(const char *trace)
207{
208 while (*trace && *trace != '\n')
209 trace++;
210 return trace;
211}
212
213static const char *skip_eols(const char *trace)
214{
215 while (*trace == '\n')
216 trace++;
217 return trace;
218}
219
220static PyObject *
221meth_get__text_signature__(PyCFunctionObject *m, void *closure)
222{
223 const char *start = find_signature(m);
224 const char *trace;
225
226 if (!start) {
227 Py_INCREF(Py_None);
228 return Py_None;
229 }
230
231 trace = skip_signature(start);
232 return PyUnicode_FromStringAndSize(start, trace - start);
233}
234
Tim Peters6d6c1a32001-08-02 04:15:00 +0000235static PyObject *
236meth_get__doc__(PyCFunctionObject *m, void *closure)
237{
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800238 const char *doc = find_signature(m);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000239
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800240 if (doc)
241 doc = skip_eols(skip_signature(doc));
242 else
243 doc = m->m_ml->ml_doc;
244
245 if (!doc) {
246 Py_INCREF(Py_None);
247 return Py_None;
248 }
249
250 return PyUnicode_FromString(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000251}
252
253static PyObject *
254meth_get__name__(PyCFunctionObject *m, void *closure)
255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000257}
258
Antoine Pitrou5b629422011-12-23 12:40:16 +0100259static PyObject *
260meth_get__qualname__(PyCFunctionObject *m, void *closure)
261{
262 /* If __self__ is a module or NULL, return m.__name__
263 (e.g. len.__qualname__ == 'len')
264
265 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
266 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
267
268 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
269 (e.g. [].append.__qualname__ == 'list.append') */
270 PyObject *type, *type_qualname, *res;
271 _Py_IDENTIFIER(__qualname__);
272
273 if (m->m_self == NULL || PyModule_Check(m->m_self))
274 return PyUnicode_FromString(m->m_ml->ml_name);
275
276 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
277
278 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
279 if (type_qualname == NULL)
280 return NULL;
281
282 if (!PyUnicode_Check(type_qualname)) {
283 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
284 "__qualname__ is not a unicode object");
285 Py_XDECREF(type_qualname);
286 return NULL;
287 }
288
289 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
290 Py_DECREF(type_qualname);
291 return res;
292}
293
Neil Schemenauer10c66922001-07-12 13:27:35 +0000294static int
295meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_VISIT(m->m_self);
298 Py_VISIT(m->m_module);
299 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000300}
301
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000306
Antoine Pitrou5b629422011-12-23 12:40:16 +0100307 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 if (self == NULL)
309 self = Py_None;
310 Py_INCREF(self);
311 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000312}
313
Guido van Rossum32d34c82001-09-20 21:45:26 +0000314static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
316 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100317 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800319 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000321};
322
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000323#define OFF(x) offsetof(PyCFunctionObject, x)
324
325static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
327 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000328};
329
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000331meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 if (m->m_self == NULL || PyModule_Check(m->m_self))
334 return PyUnicode_FromFormat("<built-in function %s>",
335 m->m_ml->ml_name);
336 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
337 m->m_ml->ml_name,
338 m->m_self->ob_type->tp_name,
339 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000342static PyObject *
343meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 PyCFunctionObject *a, *b;
346 PyObject *res;
347 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 if ((op != Py_EQ && op != Py_NE) ||
350 !PyCFunction_Check(self) ||
351 !PyCFunction_Check(other))
352 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500353 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
355 a = (PyCFunctionObject *)self;
356 b = (PyCFunctionObject *)other;
357 eq = a->m_self == b->m_self;
358 if (eq)
359 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
360 if (op == Py_EQ)
361 res = eq ? Py_True : Py_False;
362 else
363 res = eq ? Py_False : Py_True;
364 Py_INCREF(res);
365 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000366}
367
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000368static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000369meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000370{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000371 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (a->m_self == NULL)
373 x = 0;
374 else {
375 x = PyObject_Hash(a->m_self);
376 if (x == -1)
377 return -1;
378 }
379 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
380 if (y == -1)
381 return -1;
382 x ^= y;
383 if (x == -1)
384 x = -2;
385 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000386}
387
Tim Peters6d6c1a32001-08-02 04:15:00 +0000388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 PyVarObject_HEAD_INIT(&PyType_Type, 0)
391 "builtin_function_or_method",
392 sizeof(PyCFunctionObject),
393 0,
394 (destructor)meth_dealloc, /* tp_dealloc */
395 0, /* tp_print */
396 0, /* tp_getattr */
397 0, /* tp_setattr */
398 0, /* tp_reserved */
399 (reprfunc)meth_repr, /* tp_repr */
400 0, /* tp_as_number */
401 0, /* tp_as_sequence */
402 0, /* tp_as_mapping */
403 (hashfunc)meth_hash, /* tp_hash */
404 PyCFunction_Call, /* tp_call */
405 0, /* tp_str */
406 PyObject_GenericGetAttr, /* tp_getattro */
407 0, /* tp_setattro */
408 0, /* tp_as_buffer */
409 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
410 0, /* tp_doc */
411 (traverseproc)meth_traverse, /* tp_traverse */
412 0, /* tp_clear */
413 meth_richcompare, /* tp_richcompare */
414 0, /* tp_weaklistoffset */
415 0, /* tp_iter */
416 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800417 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 meth_members, /* tp_members */
419 meth_getsets, /* tp_getset */
420 0, /* tp_base */
421 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000423
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000424/* Clear out the free list */
425
Christian Heimesa156e092008-02-16 07:38:31 +0000426int
427PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 int freelist_size = numfree;
430
431 while (free_list) {
432 PyCFunctionObject *v = free_list;
433 free_list = (PyCFunctionObject *)(v->m_self);
434 PyObject_GC_Del(v);
435 numfree--;
436 }
437 assert(numfree == 0);
438 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000439}
440
441void
442PyCFunction_Fini(void)
443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000445}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000446
David Malcolm49526f42012-06-22 14:55:41 -0400447/* Print summary info about the state of the optimized allocator */
448void
449_PyCFunction_DebugMallocStats(FILE *out)
450{
451 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200452 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100453 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400454}