blob: c1a99ab26874a360384d5099fe52f6df5ebb2796 [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 Heimes6075a822008-02-06 12:44: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 Heimes6075a822008-02-06 12:44:34 +000011static int numfree = 0;
Christian Heimes5b970ad2008-02-06 13:33:44 +000012#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 Pitrouc83ea132010-05-09 14:46:46 +000019 PyCFunctionObject *op;
20 op = free_list;
21 if (op != NULL) {
22 free_list = (PyCFunctionObject *)(op->m_self);
Martin Panter646b5282016-06-21 23:58:05 +000023 (void)PyObject_INIT(op, &PyCFunction_Type);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000024 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 Pitrouc83ea132010-05-09 14:46:46 +000043 if (!PyCFunction_Check(op)) {
44 PyErr_BadInternalCall();
45 return NULL;
46 }
47 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
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 Pitrouc83ea132010-05-09 14:46:46 +000053 if (!PyCFunction_Check(op)) {
54 PyErr_BadInternalCall();
55 return NULL;
56 }
57 return ((PyCFunctionObject *)op) -> m_self;
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 Pitrouc83ea132010-05-09 14:46:46 +000063 if (!PyCFunction_Check(op)) {
64 PyErr_BadInternalCall();
65 return -1;
66 }
67 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 case METH_OLDARGS | METH_KEYWORDS:
85 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
86 case METH_NOARGS:
87 if (kw == NULL || PyDict_Size(kw) == 0) {
88 size = PyTuple_GET_SIZE(arg);
89 if (size == 0)
90 return (*meth)(self, NULL);
91 PyErr_Format(PyExc_TypeError,
92 "%.200s() takes no arguments (%zd given)",
93 f->m_ml->ml_name, size);
94 return NULL;
95 }
96 break;
97 case METH_O:
98 if (kw == NULL || PyDict_Size(kw) == 0) {
99 size = PyTuple_GET_SIZE(arg);
100 if (size == 1)
101 return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
102 PyErr_Format(PyExc_TypeError,
103 "%.200s() takes exactly one argument (%zd given)",
104 f->m_ml->ml_name, size);
105 return NULL;
106 }
107 break;
108 case METH_OLDARGS:
109 /* the really old style */
110 if (kw == NULL || PyDict_Size(kw) == 0) {
111 size = PyTuple_GET_SIZE(arg);
112 if (size == 1)
113 arg = PyTuple_GET_ITEM(arg, 0);
114 else if (size == 0)
115 arg = NULL;
116 return (*meth)(self, arg);
117 }
118 break;
119 default:
120 PyErr_BadInternalCall();
121 return NULL;
122 }
123 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
124 f->m_ml->ml_name);
125 return NULL;
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000126}
127
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128/* Methods (the standard built-in methods, that is) */
129
130static void
Fred Drakeee238b92000-07-09 06:03:25 +0000131meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 _PyObject_GC_UNTRACK(m);
134 Py_XDECREF(m->m_self);
135 Py_XDECREF(m->m_module);
136 if (numfree < PyCFunction_MAXFREELIST) {
137 m->m_self = (PyObject *)free_list;
138 free_list = m;
139 numfree++;
140 }
141 else {
142 PyObject_GC_Del(m);
143 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144}
145
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146static PyObject *
147meth_get__doc__(PyCFunctionObject *m, void *closure)
148{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000149 const char *doc = m->m_ml->ml_doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000150
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000151 if (doc != NULL)
152 return PyString_FromString(doc);
153 Py_INCREF(Py_None);
154 return Py_None;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000155}
156
157static PyObject *
158meth_get__name__(PyCFunctionObject *m, void *closure)
159{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000160 return PyString_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000161}
162
Neil Schemenauer10c66922001-07-12 13:27:35 +0000163static int
164meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
165{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166 Py_VISIT(m->m_self);
167 Py_VISIT(m->m_module);
168 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000169}
170
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000173{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 PyObject *self;
175 if (PyEval_GetRestricted()) {
176 PyErr_SetString(PyExc_RuntimeError,
177 "method.__self__ not accessible in restricted mode");
178 return NULL;
179 }
180 self = m->m_self;
181 if (self == NULL)
182 self = Py_None;
183 Py_INCREF(self);
184 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000185}
186
Guido van Rossum32d34c82001-09-20 21:45:26 +0000187static PyGetSetDef meth_getsets [] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
189 {"__name__", (getter)meth_get__name__, NULL, NULL},
190 {"__self__", (getter)meth_get__self__, NULL, NULL},
191 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192};
193
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000194#define OFF(x) offsetof(PyCFunctionObject, x)
195
196static PyMemberDef meth_members[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
198 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000199};
200
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000202meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 if (m->m_self == NULL)
205 return PyString_FromFormat("<built-in function %s>",
206 m->m_ml->ml_name);
207 return PyString_FromFormat("<built-in method %s of %s object at %p>",
208 m->m_ml->ml_name,
209 m->m_self->ob_type->tp_name,
210 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211}
212
Guido van Rossum9bfef441993-03-29 10:43:31 +0000213static int
Fred Drakeee238b92000-07-09 06:03:25 +0000214meth_compare(PyCFunctionObject *a, PyCFunctionObject *b)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000215{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 if (a->m_self != b->m_self)
217 return (a->m_self < b->m_self) ? -1 : 1;
218 if (a->m_ml->ml_meth == b->m_ml->ml_meth)
219 return 0;
220 if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
221 return -1;
222 else
223 return 1;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000224}
225
Steven Bethard6a644f92008-03-18 22:08:20 +0000226static PyObject *
227meth_richcompare(PyObject *self, PyObject *other, int op)
228{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 PyCFunctionObject *a, *b;
230 PyObject *res;
231 int eq;
Steven Bethard6a644f92008-03-18 22:08:20 +0000232
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000233 if (op != Py_EQ && op != Py_NE) {
234 /* Py3K warning if comparison isn't == or !=. */
235 if (PyErr_WarnPy3k("builtin_function_or_method order "
236 "comparisons not supported in 3.x", 1) < 0) {
237 return NULL;
238 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000239
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000240 Py_INCREF(Py_NotImplemented);
241 return Py_NotImplemented;
242 }
243 else if (!PyCFunction_Check(self) || !PyCFunction_Check(other)) {
244 Py_INCREF(Py_NotImplemented);
245 return Py_NotImplemented;
246 }
247 a = (PyCFunctionObject *)self;
248 b = (PyCFunctionObject *)other;
249 eq = a->m_self == b->m_self;
250 if (eq)
251 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
252 if (op == Py_EQ)
253 res = eq ? Py_True : Py_False;
254 else
255 res = eq ? Py_False : Py_True;
256 Py_INCREF(res);
257 return res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000258}
259
Guido van Rossum9bfef441993-03-29 10:43:31 +0000260static long
Fred Drakeee238b92000-07-09 06:03:25 +0000261meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000262{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000263 long x,y;
264 if (a->m_self == NULL)
265 x = 0;
266 else {
267 x = PyObject_Hash(a->m_self);
268 if (x == -1)
269 return -1;
270 }
271 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
272 if (y == -1)
273 return -1;
274 x ^= y;
275 if (x == -1)
276 x = -2;
277 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000278}
279
Tim Peters6d6c1a32001-08-02 04:15:00 +0000280
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281PyTypeObject PyCFunction_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282 PyVarObject_HEAD_INIT(&PyType_Type, 0)
283 "builtin_function_or_method",
284 sizeof(PyCFunctionObject),
285 0,
286 (destructor)meth_dealloc, /* tp_dealloc */
287 0, /* tp_print */
288 0, /* tp_getattr */
289 0, /* tp_setattr */
290 (cmpfunc)meth_compare, /* tp_compare */
291 (reprfunc)meth_repr, /* tp_repr */
292 0, /* tp_as_number */
293 0, /* tp_as_sequence */
294 0, /* tp_as_mapping */
295 (hashfunc)meth_hash, /* tp_hash */
296 PyCFunction_Call, /* tp_call */
297 0, /* tp_str */
298 PyObject_GenericGetAttr, /* tp_getattro */
299 0, /* tp_setattro */
300 0, /* tp_as_buffer */
301 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
302 0, /* tp_doc */
303 (traverseproc)meth_traverse, /* tp_traverse */
304 0, /* tp_clear */
305 meth_richcompare, /* tp_richcompare */
306 0, /* tp_weaklistoffset */
307 0, /* tp_iter */
308 0, /* tp_iternext */
309 0, /* tp_methods */
310 meth_members, /* tp_members */
311 meth_getsets, /* tp_getset */
312 0, /* tp_base */
313 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315
Guido van Rossum69785031995-01-26 22:58:48 +0000316/* List all methods in a chain -- helper for findmethodinchain */
Guido van Rossume9c430f1991-10-20 20:21:15 +0000317
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000319listmethodchain(PyMethodChain *chain)
Guido van Rossume9c430f1991-10-20 20:21:15 +0000320{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 PyMethodChain *c;
322 PyMethodDef *ml;
323 int i, n;
324 PyObject *v;
Tim Peters541ceec2003-01-05 07:22:44 +0000325
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 n = 0;
327 for (c = chain; c != NULL; c = c->link) {
328 for (ml = c->methods; ml->ml_name != NULL; ml++)
329 n++;
330 }
331 v = PyList_New(n);
332 if (v == NULL)
333 return NULL;
334 i = 0;
335 for (c = chain; c != NULL; c = c->link) {
336 for (ml = c->methods; ml->ml_name != NULL; ml++) {
337 PyList_SetItem(v, i, PyString_FromString(ml->ml_name));
338 i++;
339 }
340 }
341 if (PyErr_Occurred()) {
342 Py_DECREF(v);
343 return NULL;
344 }
345 PyList_Sort(v);
346 return v;
Guido van Rossume9c430f1991-10-20 20:21:15 +0000347}
348
Guido van Rossum69785031995-01-26 22:58:48 +0000349/* Find a method in a method chain */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000350
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000352Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 if (name[0] == '_' && name[1] == '_') {
355 if (strcmp(name, "__methods__") == 0) {
356 if (PyErr_WarnPy3k("__methods__ not supported in 3.x",
357 1) < 0)
358 return NULL;
359 return listmethodchain(chain);
360 }
361 if (strcmp(name, "__doc__") == 0) {
362 const char *doc = self->ob_type->tp_doc;
363 if (doc != NULL)
364 return PyString_FromString(doc);
365 }
366 }
367 while (chain != NULL) {
368 PyMethodDef *ml = chain->methods;
369 for (; ml->ml_name != NULL; ml++) {
370 if (name[0] == ml->ml_name[0] &&
371 strcmp(name+1, ml->ml_name+1) == 0)
372 /* XXX */
373 return PyCFunction_New(ml, self);
374 }
375 chain = chain->link;
376 }
377 PyErr_SetString(PyExc_AttributeError, name);
378 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379}
Guido van Rossum69785031995-01-26 22:58:48 +0000380
381/* Find a method in a single method list */
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000384Py_FindMethod(PyMethodDef *methods, PyObject *self, const char *name)
Guido van Rossum69785031995-01-26 22:58:48 +0000385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000386 PyMethodChain chain;
387 chain.methods = methods;
388 chain.link = NULL;
389 return Py_FindMethodInChain(&chain, self, name);
Guido van Rossum69785031995-01-26 22:58:48 +0000390}
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000391
392/* Clear out the free list */
393
Christian Heimes3b718a72008-02-14 12:47:33 +0000394int
395PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000396{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 int freelist_size = numfree;
398
399 while (free_list) {
400 PyCFunctionObject *v = free_list;
401 free_list = (PyCFunctionObject *)(v->m_self);
402 PyObject_GC_Del(v);
403 numfree--;
404 }
405 assert(numfree == 0);
406 return freelist_size;
Christian Heimes3b718a72008-02-14 12:47:33 +0000407}
408
409void
410PyCFunction_Fini(void)
411{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000413}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000414
415/* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
416 but it's part of the API so we need to keep a function around that
417 existing C extensions can call.
418*/
Christian Heimes6075a822008-02-06 12:44:34 +0000419
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000420#undef PyCFunction_New
421PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
422
423PyObject *
424PyCFunction_New(PyMethodDef *ml, PyObject *self)
425{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 return PyCFunction_NewEx(ml, self, NULL);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000427}