Updated for 2.1a3
[python/dscho.git] / Objects / methodobject.c
blobf0bbeeacc990f1c47e8c73e7fb9530a2c4b62f8e
2 /* Method object implementation */
4 #include "Python.h"
6 #include "token.h"
8 static PyCFunctionObject *free_list = NULL;
10 PyObject *
11 PyCFunction_New(PyMethodDef *ml, PyObject *self)
13 PyCFunctionObject *op;
14 op = free_list;
15 if (op != NULL) {
16 free_list = (PyCFunctionObject *)(op->m_self);
17 PyObject_INIT(op, &PyCFunction_Type);
19 else {
20 op = PyObject_NEW(PyCFunctionObject, &PyCFunction_Type);
21 if (op == NULL)
22 return NULL;
24 op->m_ml = ml;
25 Py_XINCREF(self);
26 op->m_self = self;
27 return (PyObject *)op;
30 PyCFunction
31 PyCFunction_GetFunction(PyObject *op)
33 if (!PyCFunction_Check(op)) {
34 PyErr_BadInternalCall();
35 return NULL;
37 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
40 PyObject *
41 PyCFunction_GetSelf(PyObject *op)
43 if (!PyCFunction_Check(op)) {
44 PyErr_BadInternalCall();
45 return NULL;
47 return ((PyCFunctionObject *)op) -> m_self;
50 int
51 PyCFunction_GetFlags(PyObject *op)
53 if (!PyCFunction_Check(op)) {
54 PyErr_BadInternalCall();
55 return -1;
57 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
60 /* Methods (the standard built-in methods, that is) */
62 static void
63 meth_dealloc(PyCFunctionObject *m)
65 Py_XDECREF(m->m_self);
66 m->m_self = (PyObject *)free_list;
67 free_list = m;
70 static PyObject *
71 meth_getattr(PyCFunctionObject *m, char *name)
73 if (strcmp(name, "__name__") == 0) {
74 return PyString_FromString(m->m_ml->ml_name);
76 if (strcmp(name, "__doc__") == 0) {
77 char *doc = m->m_ml->ml_doc;
78 if (doc != NULL)
79 return PyString_FromString(doc);
80 Py_INCREF(Py_None);
81 return Py_None;
83 if (strcmp(name, "__self__") == 0) {
84 PyObject *self;
85 if (PyEval_GetRestricted()) {
86 PyErr_SetString(PyExc_RuntimeError,
87 "method.__self__ not accessible in restricted mode");
88 return NULL;
90 self = m->m_self;
91 if (self == NULL)
92 self = Py_None;
93 Py_INCREF(self);
94 return self;
96 if (strcmp(name, "__members__") == 0) {
97 return Py_BuildValue("[sss]",
98 "__doc__", "__name__", "__self__");
100 PyErr_SetString(PyExc_AttributeError, name);
101 return NULL;
104 static PyObject *
105 meth_repr(PyCFunctionObject *m)
107 char buf[200];
108 if (m->m_self == NULL)
109 sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
110 else
111 sprintf(buf,
112 "<built-in method %.80s of %.80s object at %p>",
113 m->m_ml->ml_name, m->m_self->ob_type->tp_name,
114 m->m_self);
115 return PyString_FromString(buf);
118 static int
119 meth_compare(PyCFunctionObject *a, PyCFunctionObject *b)
121 if (a->m_self != b->m_self)
122 return (a->m_self < b->m_self) ? -1 : 1;
123 if (a->m_ml->ml_meth == b->m_ml->ml_meth)
124 return 0;
125 if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
126 return -1;
127 else
128 return 1;
131 static long
132 meth_hash(PyCFunctionObject *a)
134 long x,y;
135 if (a->m_self == NULL)
136 x = 0;
137 else {
138 x = PyObject_Hash(a->m_self);
139 if (x == -1)
140 return -1;
142 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
143 if (y == -1)
144 return -1;
145 x ^= y;
146 if (x == -1)
147 x = -2;
148 return x;
151 PyTypeObject PyCFunction_Type = {
152 PyObject_HEAD_INIT(&PyType_Type)
154 "builtin_function_or_method",
155 sizeof(PyCFunctionObject),
157 (destructor)meth_dealloc, /*tp_dealloc*/
158 0, /*tp_print*/
159 (getattrfunc)meth_getattr, /*tp_getattr*/
160 0, /*tp_setattr*/
161 (cmpfunc)meth_compare, /*tp_compare*/
162 (reprfunc)meth_repr, /*tp_repr*/
163 0, /*tp_as_number*/
164 0, /*tp_as_sequence*/
165 0, /*tp_as_mapping*/
166 (hashfunc)meth_hash, /*tp_hash*/
169 /* List all methods in a chain -- helper for findmethodinchain */
171 static PyObject *
172 listmethodchain(PyMethodChain *chain)
174 PyMethodChain *c;
175 PyMethodDef *ml;
176 int i, n;
177 PyObject *v;
179 n = 0;
180 for (c = chain; c != NULL; c = c->link) {
181 for (ml = c->methods; ml->ml_name != NULL; ml++)
182 n++;
184 v = PyList_New(n);
185 if (v == NULL)
186 return NULL;
187 i = 0;
188 for (c = chain; c != NULL; c = c->link) {
189 for (ml = c->methods; ml->ml_name != NULL; ml++) {
190 PyList_SetItem(v, i, PyString_FromString(ml->ml_name));
191 i++;
194 if (PyErr_Occurred()) {
195 Py_DECREF(v);
196 return NULL;
198 PyList_Sort(v);
199 return v;
202 /* Find a method in a method chain */
204 PyObject *
205 Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name)
207 if (name[0] == '_' && name[1] == '_') {
208 if (strcmp(name, "__methods__") == 0)
209 return listmethodchain(chain);
210 if (strcmp(name, "__doc__") == 0) {
211 char *doc = self->ob_type->tp_doc;
212 if (doc != NULL)
213 return PyString_FromString(doc);
216 while (chain != NULL) {
217 PyMethodDef *ml = chain->methods;
218 for (; ml->ml_name != NULL; ml++) {
219 if (name[0] == ml->ml_name[0] &&
220 strcmp(name+1, ml->ml_name+1) == 0)
221 return PyCFunction_New(ml, self);
223 chain = chain->link;
225 PyErr_SetString(PyExc_AttributeError, name);
226 return NULL;
229 /* Find a method in a single method list */
231 PyObject *
232 Py_FindMethod(PyMethodDef *methods, PyObject *self, char *name)
234 PyMethodChain chain;
235 chain.methods = methods;
236 chain.link = NULL;
237 return Py_FindMethodInChain(&chain, self, name);
240 /* Clear out the free list */
242 void
243 PyCFunction_Fini(void)
245 while (free_list) {
246 PyCFunctionObject *v = free_list;
247 free_list = (PyCFunctionObject *)(v->m_self);
248 PyObject_DEL(v);