blob: c3a64098dc9efc7d40a73c128e5c943f1ceee1ff [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 *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000017PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 PyCFunctionObject *op;
20 op = free_list;
21 if (op != NULL) {
22 free_list = (PyCFunctionObject *)(op->m_self);
23 PyObject_INIT(op, &PyCFunction_Type);
24 numfree--;
25 }
26 else {
27 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
28 if (op == NULL)
29 return NULL;
30 }
31 op->m_ml = ml;
32 Py_XINCREF(self);
33 op->m_self = self;
34 Py_XINCREF(module);
35 op->m_module = module;
36 _PyObject_GC_TRACK(op);
37 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038}
39
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000041PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 if (!PyCFunction_Check(op)) {
44 PyErr_BadInternalCall();
45 return NULL;
46 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010047 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048}
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000051PyCFunction_GetSelf(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_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
Guido van Rossumc0602291991-12-16 13:07:24 +000060int
Fred Drakeee238b92000-07-09 06:03:25 +000061PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 if (!PyCFunction_Check(op)) {
64 PyErr_BadInternalCall();
65 return -1;
66 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010067 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000068}
69
Jeremy Hylton910d7d42001-08-12 21:52:24 +000070PyObject *
71PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
72{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 PyCFunctionObject* f = (PyCFunctionObject*)func;
74 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
75 PyObject *self = PyCFunction_GET_SELF(func);
76 Py_ssize_t size;
Jeremy Hylton910d7d42001-08-12 21:52:24 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
79 case METH_VARARGS:
80 if (kw == NULL || PyDict_Size(kw) == 0)
81 return (*meth)(self, arg);
82 break;
83 case METH_VARARGS | METH_KEYWORDS:
84 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
85 case METH_NOARGS:
86 if (kw == NULL || PyDict_Size(kw) == 0) {
87 size = PyTuple_GET_SIZE(arg);
88 if (size == 0)
89 return (*meth)(self, NULL);
90 PyErr_Format(PyExc_TypeError,
91 "%.200s() takes no arguments (%zd given)",
92 f->m_ml->ml_name, size);
93 return NULL;
94 }
95 break;
96 case METH_O:
97 if (kw == NULL || PyDict_Size(kw) == 0) {
98 size = PyTuple_GET_SIZE(arg);
99 if (size == 1)
100 return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
101 PyErr_Format(PyExc_TypeError,
102 "%.200s() takes exactly one argument (%zd given)",
103 f->m_ml->ml_name, size);
104 return NULL;
105 }
106 break;
107 default:
108 PyErr_SetString(PyExc_SystemError, "Bad call flags in "
109 "PyCFunction_Call. METH_OLDARGS is no "
110 "longer supported!");
111
112 return NULL;
113 }
114 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
115 f->m_ml->ml_name);
116 return NULL;
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000117}
118
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119/* Methods (the standard built-in methods, that is) */
120
121static void
Fred Drakeee238b92000-07-09 06:03:25 +0000122meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 _PyObject_GC_UNTRACK(m);
125 Py_XDECREF(m->m_self);
126 Py_XDECREF(m->m_module);
127 if (numfree < PyCFunction_MAXFREELIST) {
128 m->m_self = (PyObject *)free_list;
129 free_list = m;
130 numfree++;
131 }
132 else {
133 PyObject_GC_Del(m);
134 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Tim Peters6d6c1a32001-08-02 04:15:00 +0000137static PyObject *
138meth_get__doc__(PyCFunctionObject *m, void *closure)
139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 const char *doc = m->m_ml->ml_doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (doc != NULL)
143 return PyUnicode_FromString(doc);
144 Py_INCREF(Py_None);
145 return Py_None;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146}
147
148static PyObject *
149meth_get__name__(PyCFunctionObject *m, void *closure)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152}
153
Antoine Pitrou5b629422011-12-23 12:40:16 +0100154static PyObject *
155meth_get__qualname__(PyCFunctionObject *m, void *closure)
156{
157 /* If __self__ is a module or NULL, return m.__name__
158 (e.g. len.__qualname__ == 'len')
159
160 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
161 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
162
163 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
164 (e.g. [].append.__qualname__ == 'list.append') */
165 PyObject *type, *type_qualname, *res;
166 _Py_IDENTIFIER(__qualname__);
167
168 if (m->m_self == NULL || PyModule_Check(m->m_self))
169 return PyUnicode_FromString(m->m_ml->ml_name);
170
171 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
172
173 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
174 if (type_qualname == NULL)
175 return NULL;
176
177 if (!PyUnicode_Check(type_qualname)) {
178 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
179 "__qualname__ is not a unicode object");
180 Py_XDECREF(type_qualname);
181 return NULL;
182 }
183
184 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
185 Py_DECREF(type_qualname);
186 return res;
187}
188
Neil Schemenauer10c66922001-07-12 13:27:35 +0000189static int
190meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 Py_VISIT(m->m_self);
193 Py_VISIT(m->m_module);
194 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000195}
196
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000201
Antoine Pitrou5b629422011-12-23 12:40:16 +0100202 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (self == NULL)
204 self = Py_None;
205 Py_INCREF(self);
206 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000207}
208
Guido van Rossum32d34c82001-09-20 21:45:26 +0000209static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
211 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100212 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 {"__self__", (getter)meth_get__self__, NULL, NULL},
214 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215};
216
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000217#define OFF(x) offsetof(PyCFunctionObject, x)
218
219static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
221 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000222};
223
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000225meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (m->m_self == NULL || PyModule_Check(m->m_self))
228 return PyUnicode_FromFormat("<built-in function %s>",
229 m->m_ml->ml_name);
230 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
231 m->m_ml->ml_name,
232 m->m_self->ob_type->tp_name,
233 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234}
235
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000236static PyObject *
237meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyCFunctionObject *a, *b;
240 PyObject *res;
241 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if ((op != Py_EQ && op != Py_NE) ||
244 !PyCFunction_Check(self) ||
245 !PyCFunction_Check(other))
246 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500247 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 }
249 a = (PyCFunctionObject *)self;
250 b = (PyCFunctionObject *)other;
251 eq = a->m_self == b->m_self;
252 if (eq)
253 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
254 if (op == Py_EQ)
255 res = eq ? Py_True : Py_False;
256 else
257 res = eq ? Py_False : Py_True;
258 Py_INCREF(res);
259 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000260}
261
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000262static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000263meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000265 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if (a->m_self == NULL)
267 x = 0;
268 else {
269 x = PyObject_Hash(a->m_self);
270 if (x == -1)
271 return -1;
272 }
273 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
274 if (y == -1)
275 return -1;
276 x ^= y;
277 if (x == -1)
278 x = -2;
279 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000280}
281
Tim Peters6d6c1a32001-08-02 04:15:00 +0000282
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 PyVarObject_HEAD_INIT(&PyType_Type, 0)
285 "builtin_function_or_method",
286 sizeof(PyCFunctionObject),
287 0,
288 (destructor)meth_dealloc, /* tp_dealloc */
289 0, /* tp_print */
290 0, /* tp_getattr */
291 0, /* tp_setattr */
292 0, /* tp_reserved */
293 (reprfunc)meth_repr, /* tp_repr */
294 0, /* tp_as_number */
295 0, /* tp_as_sequence */
296 0, /* tp_as_mapping */
297 (hashfunc)meth_hash, /* tp_hash */
298 PyCFunction_Call, /* tp_call */
299 0, /* tp_str */
300 PyObject_GenericGetAttr, /* tp_getattro */
301 0, /* tp_setattro */
302 0, /* tp_as_buffer */
303 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
304 0, /* tp_doc */
305 (traverseproc)meth_traverse, /* tp_traverse */
306 0, /* tp_clear */
307 meth_richcompare, /* tp_richcompare */
308 0, /* tp_weaklistoffset */
309 0, /* tp_iter */
310 0, /* tp_iternext */
311 0, /* tp_methods */
312 meth_members, /* tp_members */
313 meth_getsets, /* tp_getset */
314 0, /* tp_base */
315 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000317
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000318/* Clear out the free list */
319
Christian Heimesa156e092008-02-16 07:38:31 +0000320int
321PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 int freelist_size = numfree;
324
325 while (free_list) {
326 PyCFunctionObject *v = free_list;
327 free_list = (PyCFunctionObject *)(v->m_self);
328 PyObject_GC_Del(v);
329 numfree--;
330 }
331 assert(numfree == 0);
332 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000333}
334
335void
336PyCFunction_Fini(void)
337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000339}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000340
341/* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
342 but it's part of the API so we need to keep a function around that
343 existing C extensions can call.
344*/
Christian Heimes2202f872008-02-06 14:31:34 +0000345
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000346#undef PyCFunction_New
347PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
348
349PyObject *
350PyCFunction_New(PyMethodDef *ml, PyObject *self)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 return PyCFunction_NewEx(ml, self, NULL);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000353}