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 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 7 | /* Free list for method objects to safe malloc/free overhead |
| 8 | * The m_self element is used to chain the objects. |
| 9 | */ |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 10 | static PyCFunctionObject *free_list = NULL; |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 11 | static int numfree = 0; |
| 12 | #ifndef PyCFunction_MAXFREELIST |
| 13 | #define PyCFunction_MAXFREELIST 256 |
| 14 | #endif |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 16 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 17 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | { |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 19 | PyCFunctionObject *op; |
| 20 | op = free_list; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | if (op != NULL) { |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 22 | free_list = (PyCFunctionObject *)(op->m_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 23 | PyObject_INIT(op, &PyCFunction_Type); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 24 | numfree--; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | } |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 26 | else { |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 27 | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 28 | if (op == NULL) |
| 29 | return NULL; |
| 30 | } |
| 31 | op->m_ml = ml; |
| 32 | Py_XINCREF(self); |
| 33 | op->m_self = self; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 34 | Py_XINCREF(module); |
| 35 | op->m_module = module; |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 36 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 37 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 40 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 41 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 43 | if (!PyCFunction_Check(op)) { |
| 44 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | return NULL; |
| 46 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 47 | return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 51 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 53 | if (!PyCFunction_Check(op)) { |
| 54 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | return ((PyCFunctionObject *)op) -> m_self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 60 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 61 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 63 | if (!PyCFunction_Check(op)) { |
| 64 | PyErr_BadInternalCall(); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 65 | return -1; |
| 66 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 70 | PyObject * |
| 71 | PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 72 | { |
| 73 | PyCFunctionObject* f = (PyCFunctionObject*)func; |
| 74 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 75 | PyObject *self = PyCFunction_GET_SELF(func); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 76 | Py_ssize_t size; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 77 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 78 | switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 79 | case METH_VARARGS: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 80 | if (kw == NULL || PyDict_Size(kw) == 0) |
| 81 | return (*meth)(self, arg); |
| 82 | break; |
| 83 | case METH_VARARGS | METH_KEYWORDS: |
| 84 | return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 85 | case METH_NOARGS: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 86 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 87 | size = PyTuple_GET_SIZE(arg); |
| 88 | if (size == 0) |
| 89 | return (*meth)(self, NULL); |
| 90 | PyErr_Format(PyExc_TypeError, |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 91 | "%.200s() takes no arguments (%zd given)", |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 92 | f->m_ml->ml_name, size); |
| 93 | return NULL; |
| 94 | } |
| 95 | break; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 96 | case METH_O: |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 97 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 98 | size = PyTuple_GET_SIZE(arg); |
| 99 | if (size == 1) |
| 100 | return (*meth)(self, PyTuple_GET_ITEM(arg, 0)); |
| 101 | PyErr_Format(PyExc_TypeError, |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 102 | "%.200s() takes exactly one argument (%zd given)", |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 103 | f->m_ml->ml_name, size); |
| 104 | return NULL; |
| 105 | } |
| 106 | break; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 107 | default: |
Georg Brandl | f2fc934 | 2007-09-01 13:59:50 +0000 | [diff] [blame] | 108 | PyErr_SetString(PyExc_SystemError, "Bad call flags in " |
| 109 | "PyCFunction_Call. METH_OLDARGS is no " |
| 110 | "longer supported!"); |
| 111 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 112 | return NULL; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 113 | } |
Raymond Hettinger | bf43f8a | 2003-01-04 00:37:53 +0000 | [diff] [blame] | 114 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 115 | f->m_ml->ml_name); |
| 116 | return NULL; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | /* Methods (the standard built-in methods, that is) */ |
| 120 | |
| 121 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 122 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | { |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 124 | _PyObject_GC_UNTRACK(m); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | Py_XDECREF(m->m_self); |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 126 | Py_XDECREF(m->m_module); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 127 | if (numfree < PyCFunction_MAXFREELIST) { |
| 128 | m->m_self = (PyObject *)free_list; |
| 129 | free_list = m; |
| 130 | numfree++; |
| 131 | } |
| 132 | else { |
| 133 | PyObject_GC_Del(m); |
| 134 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 137 | static PyObject * |
| 138 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 139 | { |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 140 | const char *doc = m->m_ml->ml_doc; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 141 | |
| 142 | if (doc != NULL) |
Guido van Rossum | 928115a | 2007-08-23 23:35:15 +0000 | [diff] [blame] | 143 | return PyUnicode_FromString(doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 144 | Py_INCREF(Py_None); |
| 145 | return Py_None; |
| 146 | } |
| 147 | |
| 148 | static PyObject * |
| 149 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 150 | { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 151 | return PyUnicode_FromString(m->m_ml->ml_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 154 | static int |
| 155 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 156 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 157 | Py_VISIT(m->m_self); |
| 158 | Py_VISIT(m->m_module); |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 159 | return 0; |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 163 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 164 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 165 | PyObject *self; |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 166 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 167 | self = m->m_self; |
| 168 | if (self == NULL) |
| 169 | self = Py_None; |
| 170 | Py_INCREF(self); |
| 171 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 174 | static PyGetSetDef meth_getsets [] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 175 | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 176 | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
| 177 | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
| 178 | {0} |
| 179 | }; |
| 180 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 181 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 182 | |
| 183 | static PyMemberDef meth_members[] = { |
Christian Heimes | bce52be | 2007-11-17 19:08:41 +0000 | [diff] [blame] | 184 | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 185 | {NULL} |
| 186 | }; |
| 187 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 189 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 191 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 192 | return PyUnicode_FromFormat("<built-in function %s>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 193 | m->m_ml->ml_name); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 194 | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 195 | m->m_ml->ml_name, |
| 196 | m->m_self->ob_type->tp_name, |
| 197 | m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 200 | static PyObject * |
| 201 | meth_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 202 | { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 203 | PyCFunctionObject *a, *b; |
| 204 | PyObject *res; |
| 205 | int eq; |
| 206 | |
| 207 | if ((op != Py_EQ && op != Py_NE) || |
| 208 | !PyCFunction_Check(self) || |
| 209 | !PyCFunction_Check(other)) |
| 210 | { |
| 211 | Py_INCREF(Py_NotImplemented); |
| 212 | return Py_NotImplemented; |
| 213 | } |
| 214 | a = (PyCFunctionObject *)self; |
| 215 | b = (PyCFunctionObject *)other; |
| 216 | eq = a->m_self == b->m_self; |
| 217 | if (eq) |
| 218 | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 219 | if (op == Py_EQ) |
| 220 | res = eq ? Py_True : Py_False; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 221 | else |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 222 | res = eq ? Py_False : Py_True; |
| 223 | Py_INCREF(res); |
| 224 | return res; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static long |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 228 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 229 | { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 230 | long x,y; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 231 | if (a->m_self == NULL) |
| 232 | x = 0; |
| 233 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 234 | x = PyObject_Hash(a->m_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 235 | if (x == -1) |
| 236 | return -1; |
| 237 | } |
Guido van Rossum | 9a15c21 | 2000-06-30 22:46:04 +0000 | [diff] [blame] | 238 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 239 | if (y == -1) |
| 240 | return -1; |
| 241 | x ^= y; |
| 242 | if (x == -1) |
| 243 | x = -2; |
| 244 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | PyTypeObject PyCFunction_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 249 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 250 | "builtin_function_or_method", |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 251 | sizeof(PyCFunctionObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | 0, |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 253 | (destructor)meth_dealloc, /* tp_dealloc */ |
| 254 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 255 | 0, /* tp_getattr */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 256 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 257 | 0, /* tp_reserved */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 258 | (reprfunc)meth_repr, /* tp_repr */ |
| 259 | 0, /* tp_as_number */ |
| 260 | 0, /* tp_as_sequence */ |
| 261 | 0, /* tp_as_mapping */ |
| 262 | (hashfunc)meth_hash, /* tp_hash */ |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 263 | PyCFunction_Call, /* tp_call */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 264 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 265 | PyObject_GenericGetAttr, /* tp_getattro */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 266 | 0, /* tp_setattro */ |
| 267 | 0, /* tp_as_buffer */ |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 268 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 269 | 0, /* tp_doc */ |
| 270 | (traverseproc)meth_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 271 | 0, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 272 | meth_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 273 | 0, /* tp_weaklistoffset */ |
| 274 | 0, /* tp_iter */ |
| 275 | 0, /* tp_iternext */ |
| 276 | 0, /* tp_methods */ |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 277 | meth_members, /* tp_members */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 278 | meth_getsets, /* tp_getset */ |
| 279 | 0, /* tp_base */ |
| 280 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 283 | /* Clear out the free list */ |
| 284 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 285 | int |
| 286 | PyCFunction_ClearFreeList(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 287 | { |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 288 | int freelist_size = numfree; |
| 289 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 290 | while (free_list) { |
| 291 | PyCFunctionObject *v = free_list; |
| 292 | free_list = (PyCFunctionObject *)(v->m_self); |
Neil Schemenauer | d91eec9 | 2002-03-18 20:44:53 +0000 | [diff] [blame] | 293 | PyObject_GC_Del(v); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 294 | numfree--; |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 295 | } |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 296 | assert(numfree == 0); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 297 | return freelist_size; |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | PyCFunction_Fini(void) |
| 302 | { |
| 303 | (void)PyCFunction_ClearFreeList(); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 304 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 305 | |
| 306 | /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(), |
| 307 | but it's part of the API so we need to keep a function around that |
| 308 | existing C extensions can call. |
| 309 | */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 310 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 311 | #undef PyCFunction_New |
| 312 | PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); |
| 313 | |
| 314 | PyObject * |
| 315 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 316 | { |
| 317 | return PyCFunction_NewEx(ml, self, NULL); |
| 318 | } |