This commit was manufactured by cvs2svn to create tag
[python/dscho.git] / Objects / methodobject.c
blobe0968afc763cb9464fd0ea7a89157b80731dcf30
2 /* Method object implementation */
4 #include "Python.h"
6 static PyCFunctionObject *free_list = NULL;
8 PyObject *
9 PyCFunction_New(PyMethodDef *ml, PyObject *self)
11 PyCFunctionObject *op;
12 op = free_list;
13 if (op != NULL) {
14 free_list = (PyCFunctionObject *)(op->m_self);
15 PyObject_INIT(op, &PyCFunction_Type);
17 else {
18 op = PyObject_NEW(PyCFunctionObject, &PyCFunction_Type);
19 if (op == NULL)
20 return NULL;
22 op->m_ml = ml;
23 Py_XINCREF(self);
24 op->m_self = self;
25 PyObject_GC_Init(op);
26 return (PyObject *)op;
29 PyCFunction
30 PyCFunction_GetFunction(PyObject *op)
32 if (!PyCFunction_Check(op)) {
33 PyErr_BadInternalCall();
34 return NULL;
36 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
39 PyObject *
40 PyCFunction_GetSelf(PyObject *op)
42 if (!PyCFunction_Check(op)) {
43 PyErr_BadInternalCall();
44 return NULL;
46 return ((PyCFunctionObject *)op) -> m_self;
49 int
50 PyCFunction_GetFlags(PyObject *op)
52 if (!PyCFunction_Check(op)) {
53 PyErr_BadInternalCall();
54 return -1;
56 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
59 PyObject *
60 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
62 PyCFunctionObject* f = (PyCFunctionObject*)func;
63 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
64 PyObject *self = PyCFunction_GET_SELF(func);
65 int flags = PyCFunction_GET_FLAGS(func);
66 int size = PyTuple_GET_SIZE(arg);
68 if (flags & METH_KEYWORDS) {
69 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
71 if (kw != NULL && PyDict_Size(kw) != 0) {
72 PyErr_Format(PyExc_TypeError,
73 "%.200s() takes no keyword arguments",
74 f->m_ml->ml_name);
75 return NULL;
78 switch (flags) {
79 case METH_VARARGS:
80 return (*meth)(self, arg);
81 case METH_NOARGS:
82 if (size == 0)
83 return (*meth)(self, NULL);
84 PyErr_Format(PyExc_TypeError,
85 "%.200s() takes no arguments (%d given)",
86 f->m_ml->ml_name, size);
87 return NULL;
88 case METH_O:
89 if (size == 1)
90 return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
91 PyErr_Format(PyExc_TypeError,
92 "%.200s() takes exactly one argument (%d given)",
93 f->m_ml->ml_name, size);
94 return NULL;
95 case METH_OLDARGS:
96 /* the really old style */
97 if (size == 1)
98 arg = PyTuple_GET_ITEM(arg, 0);
99 else if (size == 0)
100 arg = NULL;
101 return (*meth)(self, arg);
102 default:
103 /* should never get here ??? */
104 PyErr_BadInternalCall();
105 return NULL;
109 /* Methods (the standard built-in methods, that is) */
111 static void
112 meth_dealloc(PyCFunctionObject *m)
114 PyObject_GC_Fini(m);
115 Py_XDECREF(m->m_self);
116 m->m_self = (PyObject *)free_list;
117 free_list = m;
120 static PyObject *
121 meth_get__doc__(PyCFunctionObject *m, void *closure)
123 char *doc = m->m_ml->ml_doc;
125 if (doc != NULL)
126 return PyString_FromString(doc);
127 Py_INCREF(Py_None);
128 return Py_None;
131 static PyObject *
132 meth_get__name__(PyCFunctionObject *m, void *closure)
134 return PyString_FromString(m->m_ml->ml_name);
137 static int
138 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
140 if (m->m_self != NULL)
141 return visit(m->m_self, arg);
142 else
143 return 0;
146 static PyObject *
147 meth_get__self__(PyCFunctionObject *m, void *closure)
149 PyObject *self;
150 if (PyEval_GetRestricted()) {
151 PyErr_SetString(PyExc_RuntimeError,
152 "method.__self__ not accessible in restricted mode");
153 return NULL;
155 self = m->m_self;
156 if (self == NULL)
157 self = Py_None;
158 Py_INCREF(self);
159 return self;
162 static PyGetSetDef meth_getsets [] = {
163 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
164 {"__name__", (getter)meth_get__name__, NULL, NULL},
165 {"__self__", (getter)meth_get__self__, NULL, NULL},
169 static PyObject *
170 meth_repr(PyCFunctionObject *m)
172 if (m->m_self == NULL)
173 return PyString_FromFormat("<built-in function %s>",
174 m->m_ml->ml_name);
175 return PyString_FromFormat("<built-in method %s of %s object at %p>",
176 m->m_ml->ml_name,
177 m->m_self->ob_type->tp_name,
178 m->m_self);
181 static int
182 meth_compare(PyCFunctionObject *a, PyCFunctionObject *b)
184 if (a->m_self != b->m_self)
185 return (a->m_self < b->m_self) ? -1 : 1;
186 if (a->m_ml->ml_meth == b->m_ml->ml_meth)
187 return 0;
188 if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
189 return -1;
190 else
191 return 1;
194 static long
195 meth_hash(PyCFunctionObject *a)
197 long x,y;
198 if (a->m_self == NULL)
199 x = 0;
200 else {
201 x = PyObject_Hash(a->m_self);
202 if (x == -1)
203 return -1;
205 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
206 if (y == -1)
207 return -1;
208 x ^= y;
209 if (x == -1)
210 x = -2;
211 return x;
215 PyTypeObject PyCFunction_Type = {
216 PyObject_HEAD_INIT(&PyType_Type)
218 "builtin_function_or_method",
219 sizeof(PyCFunctionObject) + PyGC_HEAD_SIZE,
221 (destructor)meth_dealloc, /* tp_dealloc */
222 0, /* tp_print */
223 0, /* tp_getattr */
224 0, /* tp_setattr */
225 (cmpfunc)meth_compare, /* tp_compare */
226 (reprfunc)meth_repr, /* tp_repr */
227 0, /* tp_as_number */
228 0, /* tp_as_sequence */
229 0, /* tp_as_mapping */
230 (hashfunc)meth_hash, /* tp_hash */
231 PyCFunction_Call, /* tp_call */
232 0, /* tp_str */
233 PyObject_GenericGetAttr, /* tp_getattro */
234 0, /* tp_setattro */
235 0, /* tp_as_buffer */
236 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
237 0, /* tp_doc */
238 (traverseproc)meth_traverse, /* tp_traverse */
239 0, /* tp_clear */
240 0, /* tp_richcompare */
241 0, /* tp_weaklistoffset */
242 0, /* tp_iter */
243 0, /* tp_iternext */
244 0, /* tp_methods */
245 0, /* tp_members */
246 meth_getsets, /* tp_getset */
247 0, /* tp_base */
248 0, /* tp_dict */
251 /* List all methods in a chain -- helper for findmethodinchain */
253 static PyObject *
254 listmethodchain(PyMethodChain *chain)
256 PyMethodChain *c;
257 PyMethodDef *ml;
258 int i, n;
259 PyObject *v;
261 n = 0;
262 for (c = chain; c != NULL; c = c->link) {
263 for (ml = c->methods; ml->ml_name != NULL; ml++)
264 n++;
266 v = PyList_New(n);
267 if (v == NULL)
268 return NULL;
269 i = 0;
270 for (c = chain; c != NULL; c = c->link) {
271 for (ml = c->methods; ml->ml_name != NULL; ml++) {
272 PyList_SetItem(v, i, PyString_FromString(ml->ml_name));
273 i++;
276 if (PyErr_Occurred()) {
277 Py_DECREF(v);
278 return NULL;
280 PyList_Sort(v);
281 return v;
284 /* Find a method in a method chain */
286 PyObject *
287 Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name)
289 if (name[0] == '_' && name[1] == '_') {
290 if (strcmp(name, "__methods__") == 0)
291 return listmethodchain(chain);
292 if (strcmp(name, "__doc__") == 0) {
293 char *doc = self->ob_type->tp_doc;
294 if (doc != NULL)
295 return PyString_FromString(doc);
298 while (chain != NULL) {
299 PyMethodDef *ml = chain->methods;
300 for (; ml->ml_name != NULL; ml++) {
301 if (name[0] == ml->ml_name[0] &&
302 strcmp(name+1, ml->ml_name+1) == 0)
303 return PyCFunction_New(ml, self);
305 chain = chain->link;
307 PyErr_SetString(PyExc_AttributeError, name);
308 return NULL;
311 /* Find a method in a single method list */
313 PyObject *
314 Py_FindMethod(PyMethodDef *methods, PyObject *self, char *name)
316 PyMethodChain chain;
317 chain.methods = methods;
318 chain.link = NULL;
319 return Py_FindMethodInChain(&chain, self, name);
322 /* Clear out the free list */
324 void
325 PyCFunction_Fini(void)
327 while (free_list) {
328 PyCFunctionObject *v = free_list;
329 free_list = (PyCFunctionObject *)(v->m_self);
330 v = (PyCFunctionObject *) PyObject_AS_GC(v);
331 PyObject_DEL(v);