blob: 5e920a2c3215541347bf0dd4ce6aa7b27eec7db9 [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
Guido van Rossum1f39c5c1997-08-05 02:11:41 +00007static PyCFunctionObject *free_list = NULL;
8
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000010PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000012 PyCFunctionObject *op;
13 op = free_list;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014 if (op != NULL) {
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000015 free_list = (PyCFunctionObject *)(op->m_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +000016 PyObject_INIT(op, &PyCFunction_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017 }
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000018 else {
Neil Schemenauerd91eec92002-03-18 20:44:53 +000019 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000020 if (op == NULL)
21 return NULL;
22 }
23 op->m_ml = ml;
24 Py_XINCREF(self);
25 op->m_self = self;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000026 Py_XINCREF(module);
27 op->m_module = module;
Neil Schemenauerd91eec92002-03-18 20:44:53 +000028 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030}
31
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000033PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035 if (!PyCFunction_Check(op)) {
36 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037 return NULL;
38 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040}
41
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000043PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045 if (!PyCFunction_Check(op)) {
46 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047 return NULL;
48 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049 return ((PyCFunctionObject *)op) -> m_self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050}
51
Guido van Rossumc0602291991-12-16 13:07:24 +000052int
Fred Drakeee238b92000-07-09 06:03:25 +000053PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000054{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000055 if (!PyCFunction_Check(op)) {
56 PyErr_BadInternalCall();
Guido van Rossumc0602291991-12-16 13:07:24 +000057 return -1;
58 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
Guido van Rossumc0602291991-12-16 13:07:24 +000060}
61
Jeremy Hylton910d7d42001-08-12 21:52:24 +000062PyObject *
63PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
64{
65 PyCFunctionObject* f = (PyCFunctionObject*)func;
66 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
67 PyObject *self = PyCFunction_GET_SELF(func);
Martin v. Löwis18e16552006-02-15 17:27:45 +000068 long size;
Jeremy Hylton910d7d42001-08-12 21:52:24 +000069
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +000070 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000071 case METH_VARARGS:
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +000072 if (kw == NULL || PyDict_Size(kw) == 0)
73 return (*meth)(self, arg);
74 break;
75 case METH_VARARGS | METH_KEYWORDS:
Tim Peters541ceec2003-01-05 07:22:44 +000076 case METH_OLDARGS | METH_KEYWORDS:
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +000077 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000078 case METH_NOARGS:
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +000079 if (kw == NULL || PyDict_Size(kw) == 0) {
80 size = PyTuple_GET_SIZE(arg);
81 if (size == 0)
82 return (*meth)(self, NULL);
83 PyErr_Format(PyExc_TypeError,
Martin v. Löwis18e16552006-02-15 17:27:45 +000084 "%.200s() takes no arguments (%ld given)",
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +000085 f->m_ml->ml_name, size);
86 return NULL;
87 }
88 break;
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000089 case METH_O:
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +000090 if (kw == NULL || PyDict_Size(kw) == 0) {
91 size = PyTuple_GET_SIZE(arg);
92 if (size == 1)
93 return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
94 PyErr_Format(PyExc_TypeError,
Martin v. Löwis18e16552006-02-15 17:27:45 +000095 "%.200s() takes exactly one argument (%ld given)",
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +000096 f->m_ml->ml_name, size);
97 return NULL;
98 }
99 break;
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000100 case METH_OLDARGS:
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000101 /* the really old style */
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +0000102 if (kw == NULL || PyDict_Size(kw) == 0) {
103 size = PyTuple_GET_SIZE(arg);
104 if (size == 1)
105 arg = PyTuple_GET_ITEM(arg, 0);
106 else if (size == 0)
107 arg = NULL;
108 return (*meth)(self, arg);
109 }
110 break;
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000111 default:
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000112 PyErr_BadInternalCall();
113 return NULL;
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000114 }
Raymond Hettingerbf43f8a2003-01-04 00:37:53 +0000115 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
116 f->m_ml->ml_name);
117 return NULL;
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000118}
119
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120/* Methods (the standard built-in methods, that is) */
121
122static void
Fred Drakeee238b92000-07-09 06:03:25 +0000123meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124{
Neil Schemenauerd91eec92002-03-18 20:44:53 +0000125 _PyObject_GC_UNTRACK(m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 Py_XDECREF(m->m_self);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000127 Py_XDECREF(m->m_module);
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000128 m->m_self = (PyObject *)free_list;
129 free_list = m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Tim Peters6d6c1a32001-08-02 04:15:00 +0000132static PyObject *
133meth_get__doc__(PyCFunctionObject *m, void *closure)
134{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000135 const char *doc = m->m_ml->ml_doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136
137 if (doc != NULL)
138 return PyString_FromString(doc);
139 Py_INCREF(Py_None);
140 return Py_None;
141}
142
143static PyObject *
144meth_get__name__(PyCFunctionObject *m, void *closure)
145{
146 return PyString_FromString(m->m_ml->ml_name);
147}
148
Neil Schemenauer10c66922001-07-12 13:27:35 +0000149static int
150meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
151{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000152 int err;
153 if (m->m_self != NULL) {
154 err = visit(m->m_self, arg);
155 if (err)
156 return err;
157 }
158 if (m->m_module != NULL) {
159 err = visit(m->m_module, arg);
160 if (err)
161 return err;
162 }
163 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000164}
165
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000167meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000168{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000169 PyObject *self;
170 if (PyEval_GetRestricted()) {
171 PyErr_SetString(PyExc_RuntimeError,
172 "method.__self__ not accessible in restricted mode");
173 return NULL;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000174 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000175 self = m->m_self;
176 if (self == NULL)
177 self = Py_None;
178 Py_INCREF(self);
179 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000180}
181
Guido van Rossum32d34c82001-09-20 21:45:26 +0000182static PyGetSetDef meth_getsets [] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
184 {"__name__", (getter)meth_get__name__, NULL, NULL},
185 {"__self__", (getter)meth_get__self__, NULL, NULL},
186 {0}
187};
188
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000189#define OFF(x) offsetof(PyCFunctionObject, x)
190
191static PyMemberDef meth_members[] = {
Guido van Rossum6b29c012003-02-18 17:18:35 +0000192 {"__module__", T_OBJECT, OFF(m_module), WRITE_RESTRICTED},
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000193 {NULL}
194};
195
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000197meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 if (m->m_self == NULL)
Barry Warsaw7ce36942001-08-24 18:34:26 +0000200 return PyString_FromFormat("<built-in function %s>",
201 m->m_ml->ml_name);
202 return PyString_FromFormat("<built-in method %s of %s object at %p>",
203 m->m_ml->ml_name,
204 m->m_self->ob_type->tp_name,
205 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206}
207
Guido van Rossum9bfef441993-03-29 10:43:31 +0000208static int
Fred Drakeee238b92000-07-09 06:03:25 +0000209meth_compare(PyCFunctionObject *a, PyCFunctionObject *b)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000210{
211 if (a->m_self != b->m_self)
Guido van Rossum13fdf5e1998-12-04 18:52:55 +0000212 return (a->m_self < b->m_self) ? -1 : 1;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000213 if (a->m_ml->ml_meth == b->m_ml->ml_meth)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000214 return 0;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000215 if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000216 return -1;
217 else
218 return 1;
219}
220
221static long
Fred Drakeee238b92000-07-09 06:03:25 +0000222meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000223{
Fred Drake13634cf2000-06-29 19:17:04 +0000224 long x,y;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000225 if (a->m_self == NULL)
226 x = 0;
227 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228 x = PyObject_Hash(a->m_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000229 if (x == -1)
230 return -1;
231 }
Guido van Rossum9a15c212000-06-30 22:46:04 +0000232 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
Fred Drake13634cf2000-06-29 19:17:04 +0000233 if (y == -1)
234 return -1;
235 x ^= y;
236 if (x == -1)
237 x = -2;
238 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000239}
240
Tim Peters6d6c1a32001-08-02 04:15:00 +0000241
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242PyTypeObject PyCFunction_Type = {
243 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 0,
Guido van Rossum7066dd71992-09-17 17:54:56 +0000245 "builtin_function_or_method",
Neil Schemenauerd91eec92002-03-18 20:44:53 +0000246 sizeof(PyCFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 0,
Neil Schemenauer10c66922001-07-12 13:27:35 +0000248 (destructor)meth_dealloc, /* tp_dealloc */
249 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000250 0, /* tp_getattr */
Neil Schemenauer10c66922001-07-12 13:27:35 +0000251 0, /* tp_setattr */
252 (cmpfunc)meth_compare, /* tp_compare */
253 (reprfunc)meth_repr, /* tp_repr */
254 0, /* tp_as_number */
255 0, /* tp_as_sequence */
256 0, /* tp_as_mapping */
257 (hashfunc)meth_hash, /* tp_hash */
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000258 PyCFunction_Call, /* tp_call */
Neil Schemenauer10c66922001-07-12 13:27:35 +0000259 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000260 PyObject_GenericGetAttr, /* tp_getattro */
Neil Schemenauer10c66922001-07-12 13:27:35 +0000261 0, /* tp_setattro */
262 0, /* tp_as_buffer */
Neil Schemenauerd91eec92002-03-18 20:44:53 +0000263 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Neil Schemenauer10c66922001-07-12 13:27:35 +0000264 0, /* tp_doc */
265 (traverseproc)meth_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000266 0, /* tp_clear */
267 0, /* tp_richcompare */
268 0, /* tp_weaklistoffset */
269 0, /* tp_iter */
270 0, /* tp_iternext */
271 0, /* tp_methods */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000272 meth_members, /* tp_members */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000273 meth_getsets, /* tp_getset */
274 0, /* tp_base */
275 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000277
Guido van Rossum69785031995-01-26 22:58:48 +0000278/* List all methods in a chain -- helper for findmethodinchain */
Guido van Rossume9c430f1991-10-20 20:21:15 +0000279
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000281listmethodchain(PyMethodChain *chain)
Guido van Rossume9c430f1991-10-20 20:21:15 +0000282{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283 PyMethodChain *c;
284 PyMethodDef *ml;
Guido van Rossume9c430f1991-10-20 20:21:15 +0000285 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286 PyObject *v;
Tim Peters541ceec2003-01-05 07:22:44 +0000287
Guido van Rossum69785031995-01-26 22:58:48 +0000288 n = 0;
289 for (c = chain; c != NULL; c = c->link) {
290 for (ml = c->methods; ml->ml_name != NULL; ml++)
291 n++;
292 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 v = PyList_New(n);
Guido van Rossum69785031995-01-26 22:58:48 +0000294 if (v == NULL)
295 return NULL;
296 i = 0;
297 for (c = chain; c != NULL; c = c->link) {
298 for (ml = c->methods; ml->ml_name != NULL; ml++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299 PyList_SetItem(v, i, PyString_FromString(ml->ml_name));
Guido van Rossum69785031995-01-26 22:58:48 +0000300 i++;
Guido van Rossume9c430f1991-10-20 20:21:15 +0000301 }
302 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303 if (PyErr_Occurred()) {
304 Py_DECREF(v);
Guido van Rossum69785031995-01-26 22:58:48 +0000305 return NULL;
306 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307 PyList_Sort(v);
Guido van Rossume9c430f1991-10-20 20:21:15 +0000308 return v;
309}
310
Guido van Rossum69785031995-01-26 22:58:48 +0000311/* Find a method in a method chain */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000312
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000314Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315{
Guido van Rossum8a92c621998-06-27 18:28:59 +0000316 if (name[0] == '_' && name[1] == '_') {
317 if (strcmp(name, "__methods__") == 0)
318 return listmethodchain(chain);
319 if (strcmp(name, "__doc__") == 0) {
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000320 const char *doc = self->ob_type->tp_doc;
Guido van Rossum8a92c621998-06-27 18:28:59 +0000321 if (doc != NULL)
322 return PyString_FromString(doc);
323 }
324 }
Guido van Rossum69785031995-01-26 22:58:48 +0000325 while (chain != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 PyMethodDef *ml = chain->methods;
Guido van Rossum69785031995-01-26 22:58:48 +0000327 for (; ml->ml_name != NULL; ml++) {
328 if (name[0] == ml->ml_name[0] &&
329 strcmp(name+1, ml->ml_name+1) == 0)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000330 /* XXX */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 return PyCFunction_New(ml, self);
Guido van Rossum69785031995-01-26 22:58:48 +0000332 }
333 chain = chain->link;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 PyErr_SetString(PyExc_AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336 return NULL;
337}
Guido van Rossum69785031995-01-26 22:58:48 +0000338
339/* Find a method in a single method list */
340
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000342Py_FindMethod(PyMethodDef *methods, PyObject *self, const char *name)
Guido van Rossum69785031995-01-26 22:58:48 +0000343{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344 PyMethodChain chain;
Guido van Rossum69785031995-01-26 22:58:48 +0000345 chain.methods = methods;
346 chain.link = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 return Py_FindMethodInChain(&chain, self, name);
Guido van Rossum69785031995-01-26 22:58:48 +0000348}
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000349
350/* Clear out the free list */
351
352void
Fred Drakeee238b92000-07-09 06:03:25 +0000353PyCFunction_Fini(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000354{
355 while (free_list) {
356 PyCFunctionObject *v = free_list;
357 free_list = (PyCFunctionObject *)(v->m_self);
Neil Schemenauerd91eec92002-03-18 20:44:53 +0000358 PyObject_GC_Del(v);
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000359 }
360}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000361
362/* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
363 but it's part of the API so we need to keep a function around that
364 existing C extensions can call.
365*/
366
367#undef PyCFunction_New
368PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
369
370PyObject *
371PyCFunction_New(PyMethodDef *ml, PyObject *self)
372{
373 return PyCFunction_NewEx(ml, self, NULL);
374}