Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Method object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 7 | static PyCFunctionObject *free_list = NULL; |
| 8 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 9 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 10 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | { |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 12 | PyCFunctionObject *op; |
| 13 | op = free_list; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | if (op != NULL) { |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 15 | free_list = (PyCFunctionObject *)(op->m_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 16 | PyObject_INIT(op, &PyCFunction_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | } |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 18 | else { |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 19 | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 20 | if (op == NULL) |
| 21 | return NULL; |
| 22 | } |
| 23 | op->m_ml = ml; |
| 24 | Py_XINCREF(self); |
| 25 | op->m_self = self; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 26 | Py_XINCREF(module); |
| 27 | op->m_module = module; |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 28 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 29 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 32 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 33 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 35 | if (!PyCFunction_Check(op)) { |
| 36 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | return NULL; |
| 38 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 39 | return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 43 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | if (!PyCFunction_Check(op)) { |
| 46 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 49 | return ((PyCFunctionObject *)op) -> m_self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 52 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 53 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 54 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | if (!PyCFunction_Check(op)) { |
| 56 | PyErr_BadInternalCall(); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 62 | PyObject * |
| 63 | PyCFunction_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); |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 68 | int size; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 69 | |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 70 | switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC)) { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 71 | case METH_VARARGS: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 72 | if (kw == NULL || PyDict_Size(kw) == 0) |
| 73 | return (*meth)(self, arg); |
| 74 | break; |
| 75 | case METH_VARARGS | METH_KEYWORDS: |
Tim Peters | 541ceec | 2003-01-05 07:22:44 +0000 | [diff] [blame] | 76 | case METH_OLDARGS | METH_KEYWORDS: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 77 | return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 78 | case METH_NOARGS: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 79 | 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, |
| 84 | "%.200s() takes no arguments (%d given)", |
| 85 | f->m_ml->ml_name, size); |
| 86 | return NULL; |
| 87 | } |
| 88 | break; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 89 | case METH_O: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 90 | 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, |
| 95 | "%.200s() takes exactly one argument (%d given)", |
| 96 | f->m_ml->ml_name, size); |
| 97 | return NULL; |
| 98 | } |
| 99 | break; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 100 | case METH_OLDARGS: |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 101 | /* the really old style */ |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 102 | 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öwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 111 | default: |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 112 | PyErr_BadInternalCall(); |
| 113 | return NULL; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 114 | } |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 115 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 116 | f->m_ml->ml_name); |
| 117 | return NULL; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | /* Methods (the standard built-in methods, that is) */ |
| 121 | |
| 122 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 123 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | { |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 125 | _PyObject_GC_UNTRACK(m); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | Py_XDECREF(m->m_self); |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 127 | Py_XDECREF(m->m_module); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 128 | m->m_self = (PyObject *)free_list; |
| 129 | free_list = m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 132 | static PyObject * |
| 133 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 134 | { |
| 135 | char *doc = m->m_ml->ml_doc; |
| 136 | |
| 137 | if (doc != NULL) |
| 138 | return PyString_FromString(doc); |
| 139 | Py_INCREF(Py_None); |
| 140 | return Py_None; |
| 141 | } |
| 142 | |
| 143 | static PyObject * |
| 144 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 145 | { |
| 146 | return PyString_FromString(m->m_ml->ml_name); |
| 147 | } |
| 148 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 149 | static int |
| 150 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 151 | { |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 152 | 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 Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 166 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 167 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 168 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 169 | PyObject *self; |
| 170 | if (PyEval_GetRestricted()) { |
| 171 | PyErr_SetString(PyExc_RuntimeError, |
| 172 | "method.__self__ not accessible in restricted mode"); |
| 173 | return NULL; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 174 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 175 | self = m->m_self; |
| 176 | if (self == NULL) |
| 177 | self = Py_None; |
| 178 | Py_INCREF(self); |
| 179 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 182 | static PyGetSetDef meth_getsets [] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 183 | {"__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 Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 189 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 190 | |
| 191 | static PyMemberDef meth_members[] = { |
Jeremy Hylton | ff71c98 | 2003-02-18 17:02:15 +0000 | [diff] [blame^] | 192 | {"__module__", T_OBJECT, OFF(m_module)}, |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 193 | {NULL} |
| 194 | }; |
| 195 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 196 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 197 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 198 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 199 | if (m->m_self == NULL) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 200 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 208 | static int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 209 | meth_compare(PyCFunctionObject *a, PyCFunctionObject *b) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 210 | { |
| 211 | if (a->m_self != b->m_self) |
Guido van Rossum | 13fdf5e | 1998-12-04 18:52:55 +0000 | [diff] [blame] | 212 | return (a->m_self < b->m_self) ? -1 : 1; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 213 | if (a->m_ml->ml_meth == b->m_ml->ml_meth) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 214 | return 0; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 215 | if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 216 | return -1; |
| 217 | else |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | static long |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 222 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 223 | { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 224 | long x,y; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 225 | if (a->m_self == NULL) |
| 226 | x = 0; |
| 227 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | x = PyObject_Hash(a->m_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 229 | if (x == -1) |
| 230 | return -1; |
| 231 | } |
Guido van Rossum | 9a15c21 | 2000-06-30 22:46:04 +0000 | [diff] [blame] | 232 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 233 | if (y == -1) |
| 234 | return -1; |
| 235 | x ^= y; |
| 236 | if (x == -1) |
| 237 | x = -2; |
| 238 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | PyTypeObject PyCFunction_Type = { |
| 243 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | 0, |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 245 | "builtin_function_or_method", |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 246 | sizeof(PyCFunctionObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | 0, |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 248 | (destructor)meth_dealloc, /* tp_dealloc */ |
| 249 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 250 | 0, /* tp_getattr */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 251 | 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 Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 258 | PyCFunction_Call, /* tp_call */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 259 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 260 | PyObject_GenericGetAttr, /* tp_getattro */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 261 | 0, /* tp_setattro */ |
| 262 | 0, /* tp_as_buffer */ |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 263 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 264 | 0, /* tp_doc */ |
| 265 | (traverseproc)meth_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 266 | 0, /* tp_clear */ |
| 267 | 0, /* tp_richcompare */ |
| 268 | 0, /* tp_weaklistoffset */ |
| 269 | 0, /* tp_iter */ |
| 270 | 0, /* tp_iternext */ |
| 271 | 0, /* tp_methods */ |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 272 | meth_members, /* tp_members */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 273 | meth_getsets, /* tp_getset */ |
| 274 | 0, /* tp_base */ |
| 275 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 278 | /* List all methods in a chain -- helper for findmethodinchain */ |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 280 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 281 | listmethodchain(PyMethodChain *chain) |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 282 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | PyMethodChain *c; |
| 284 | PyMethodDef *ml; |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 285 | int i, n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 286 | PyObject *v; |
Tim Peters | 541ceec | 2003-01-05 07:22:44 +0000 | [diff] [blame] | 287 | |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 288 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 293 | v = PyList_New(n); |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 294 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | PyList_SetItem(v, i, PyString_FromString(ml->ml_name)); |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 300 | i++; |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 303 | if (PyErr_Occurred()) { |
| 304 | Py_DECREF(v); |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | PyList_Sort(v); |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 308 | return v; |
| 309 | } |
| 310 | |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 311 | /* Find a method in a method chain */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 314 | Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 315 | { |
Guido van Rossum | 8a92c62 | 1998-06-27 18:28:59 +0000 | [diff] [blame] | 316 | if (name[0] == '_' && name[1] == '_') { |
| 317 | if (strcmp(name, "__methods__") == 0) |
| 318 | return listmethodchain(chain); |
| 319 | if (strcmp(name, "__doc__") == 0) { |
| 320 | char *doc = self->ob_type->tp_doc; |
| 321 | if (doc != NULL) |
| 322 | return PyString_FromString(doc); |
| 323 | } |
| 324 | } |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 325 | while (chain != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 326 | PyMethodDef *ml = chain->methods; |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 327 | for (; ml->ml_name != NULL; ml++) { |
| 328 | if (name[0] == ml->ml_name[0] && |
| 329 | strcmp(name+1, ml->ml_name+1) == 0) |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 330 | /* XXX */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | return PyCFunction_New(ml, self); |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 332 | } |
| 333 | chain = chain->link; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 334 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 335 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 336 | return NULL; |
| 337 | } |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 338 | |
| 339 | /* Find a method in a single method list */ |
| 340 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 342 | Py_FindMethod(PyMethodDef *methods, PyObject *self, char *name) |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 343 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 344 | PyMethodChain chain; |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 345 | chain.methods = methods; |
| 346 | chain.link = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 347 | return Py_FindMethodInChain(&chain, self, name); |
Guido van Rossum | 6978503 | 1995-01-26 22:58:48 +0000 | [diff] [blame] | 348 | } |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 349 | |
| 350 | /* Clear out the free list */ |
| 351 | |
| 352 | void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 353 | PyCFunction_Fini(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 354 | { |
| 355 | while (free_list) { |
| 356 | PyCFunctionObject *v = free_list; |
| 357 | free_list = (PyCFunctionObject *)(v->m_self); |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 358 | PyObject_GC_Del(v); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 361 | |
| 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 |
| 368 | PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); |
| 369 | |
| 370 | PyObject * |
| 371 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 372 | { |
| 373 | return PyCFunction_NewEx(ml, self, NULL); |
| 374 | } |