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 | |
Andrew Svetlov | 4de2924 | 2012-12-26 23:08:54 +0200 | [diff] [blame] | 16 | /* undefine macro trampoline to PyCFunction_NewEx */ |
| 17 | #undef PyCFunction_New |
| 18 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 19 | PyObject * |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 20 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 21 | { |
| 22 | return PyCFunction_NewEx(ml, self, NULL); |
| 23 | } |
| 24 | |
| 25 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 26 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | PyCFunctionObject *op; |
| 29 | op = free_list; |
| 30 | if (op != NULL) { |
| 31 | free_list = (PyCFunctionObject *)(op->m_self); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 32 | (void)PyObject_INIT(op, &PyCFunction_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | numfree--; |
| 34 | } |
| 35 | else { |
| 36 | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
| 37 | if (op == NULL) |
| 38 | return NULL; |
| 39 | } |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame^] | 40 | op->m_weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | op->m_ml = ml; |
| 42 | Py_XINCREF(self); |
| 43 | op->m_self = self; |
| 44 | Py_XINCREF(module); |
| 45 | op->m_module = module; |
| 46 | _PyObject_GC_TRACK(op); |
| 47 | return (PyObject *)op; |
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 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 51 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | if (!PyCFunction_Check(op)) { |
| 54 | PyErr_BadInternalCall(); |
| 55 | return NULL; |
| 56 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 57 | return PyCFunction_GET_FUNCTION(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 60 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 61 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | if (!PyCFunction_Check(op)) { |
| 64 | PyErr_BadInternalCall(); |
| 65 | return NULL; |
| 66 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 67 | return PyCFunction_GET_SELF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 70 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 71 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 72 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | if (!PyCFunction_Check(op)) { |
| 74 | PyErr_BadInternalCall(); |
| 75 | return -1; |
| 76 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 77 | return PyCFunction_GET_FLAGS(op); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 80 | PyObject * |
| 81 | PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 82 | { |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 83 | #define CHECK_RESULT(res) assert(res != NULL || PyErr_Occurred()) |
| 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | PyCFunctionObject* f = (PyCFunctionObject*)func; |
| 86 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 87 | PyObject *self = PyCFunction_GET_SELF(func); |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 88 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | Py_ssize_t size; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 90 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { |
| 92 | case METH_VARARGS: |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 93 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 94 | res = (*meth)(self, arg); |
| 95 | CHECK_RESULT(res); |
| 96 | return res; |
| 97 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | break; |
| 99 | case METH_VARARGS | METH_KEYWORDS: |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 100 | res = (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
| 101 | CHECK_RESULT(res); |
| 102 | return res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | case METH_NOARGS: |
| 104 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 105 | size = PyTuple_GET_SIZE(arg); |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 106 | if (size == 0) { |
| 107 | res = (*meth)(self, NULL); |
| 108 | CHECK_RESULT(res); |
| 109 | return res; |
| 110 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | PyErr_Format(PyExc_TypeError, |
| 112 | "%.200s() takes no arguments (%zd given)", |
| 113 | f->m_ml->ml_name, size); |
| 114 | return NULL; |
| 115 | } |
| 116 | break; |
| 117 | case METH_O: |
| 118 | if (kw == NULL || PyDict_Size(kw) == 0) { |
| 119 | size = PyTuple_GET_SIZE(arg); |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 120 | if (size == 1) { |
| 121 | res = (*meth)(self, PyTuple_GET_ITEM(arg, 0)); |
| 122 | CHECK_RESULT(res); |
| 123 | return res; |
| 124 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | PyErr_Format(PyExc_TypeError, |
| 126 | "%.200s() takes exactly one argument (%zd given)", |
| 127 | f->m_ml->ml_name, size); |
| 128 | return NULL; |
| 129 | } |
| 130 | break; |
| 131 | default: |
| 132 | PyErr_SetString(PyExc_SystemError, "Bad call flags in " |
| 133 | "PyCFunction_Call. METH_OLDARGS is no " |
| 134 | "longer supported!"); |
| 135 | |
| 136 | return NULL; |
| 137 | } |
| 138 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 139 | f->m_ml->ml_name); |
| 140 | return NULL; |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 141 | |
| 142 | #undef CHECK_RESULT |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | /* Methods (the standard built-in methods, that is) */ |
| 146 | |
| 147 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 148 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | _PyObject_GC_UNTRACK(m); |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame^] | 151 | if (m->m_weakreflist != NULL) { |
| 152 | PyObject_ClearWeakRefs((PyObject*) m); |
| 153 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | Py_XDECREF(m->m_self); |
| 155 | Py_XDECREF(m->m_module); |
| 156 | if (numfree < PyCFunction_MAXFREELIST) { |
| 157 | m->m_self = (PyObject *)free_list; |
| 158 | free_list = m; |
| 159 | numfree++; |
| 160 | } |
| 161 | else { |
| 162 | PyObject_GC_Del(m); |
| 163 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 166 | static PyObject * |
| 167 | meth_reduce(PyCFunctionObject *m) |
| 168 | { |
| 169 | PyObject *builtins; |
| 170 | PyObject *getattr; |
| 171 | _Py_IDENTIFIER(getattr); |
| 172 | |
| 173 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 174 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 175 | |
| 176 | builtins = PyEval_GetBuiltins(); |
| 177 | getattr = _PyDict_GetItemId(builtins, &PyId_getattr); |
| 178 | return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name); |
| 179 | } |
| 180 | |
| 181 | static PyMethodDef meth_methods[] = { |
| 182 | {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL}, |
| 183 | {NULL, NULL} |
| 184 | }; |
| 185 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 186 | static PyObject * |
| 187 | meth_get__text_signature__(PyCFunctionObject *m, void *closure) |
| 188 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 189 | return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 192 | static PyObject * |
| 193 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 194 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 195 | return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static PyObject * |
| 199 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | return PyUnicode_FromString(m->m_ml->ml_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 204 | static PyObject * |
| 205 | meth_get__qualname__(PyCFunctionObject *m, void *closure) |
| 206 | { |
| 207 | /* If __self__ is a module or NULL, return m.__name__ |
| 208 | (e.g. len.__qualname__ == 'len') |
| 209 | |
| 210 | If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__ |
| 211 | (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys') |
| 212 | |
| 213 | Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ |
| 214 | (e.g. [].append.__qualname__ == 'list.append') */ |
| 215 | PyObject *type, *type_qualname, *res; |
| 216 | _Py_IDENTIFIER(__qualname__); |
| 217 | |
| 218 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 219 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 220 | |
| 221 | type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); |
| 222 | |
| 223 | type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); |
| 224 | if (type_qualname == NULL) |
| 225 | return NULL; |
| 226 | |
| 227 | if (!PyUnicode_Check(type_qualname)) { |
| 228 | PyErr_SetString(PyExc_TypeError, "<method>.__class__." |
| 229 | "__qualname__ is not a unicode object"); |
| 230 | Py_XDECREF(type_qualname); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name); |
| 235 | Py_DECREF(type_qualname); |
| 236 | return res; |
| 237 | } |
| 238 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 239 | static int |
| 240 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 241 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | Py_VISIT(m->m_self); |
| 243 | Py_VISIT(m->m_module); |
| 244 | return 0; |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 248 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | PyObject *self; |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 252 | self = PyCFunction_GET_SELF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | if (self == NULL) |
| 254 | self = Py_None; |
| 255 | Py_INCREF(self); |
| 256 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 259 | static PyGetSetDef meth_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 261 | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 262 | {"__qualname__", (getter)meth_get__qualname__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 264 | {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 268 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 269 | |
| 270 | static PyMemberDef meth_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
| 272 | {NULL} |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 275 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 276 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 279 | return PyUnicode_FromFormat("<built-in function %s>", |
| 280 | m->m_ml->ml_name); |
| 281 | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
| 282 | m->m_ml->ml_name, |
| 283 | m->m_self->ob_type->tp_name, |
| 284 | m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 287 | static PyObject * |
| 288 | meth_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | PyCFunctionObject *a, *b; |
| 291 | PyObject *res; |
| 292 | int eq; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | if ((op != Py_EQ && op != Py_NE) || |
| 295 | !PyCFunction_Check(self) || |
| 296 | !PyCFunction_Check(other)) |
| 297 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 298 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | } |
| 300 | a = (PyCFunctionObject *)self; |
| 301 | b = (PyCFunctionObject *)other; |
| 302 | eq = a->m_self == b->m_self; |
| 303 | if (eq) |
| 304 | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 305 | if (op == Py_EQ) |
| 306 | res = eq ? Py_True : Py_False; |
| 307 | else |
| 308 | res = eq ? Py_False : Py_True; |
| 309 | Py_INCREF(res); |
| 310 | return res; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 313 | static Py_hash_t |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 314 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 315 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 316 | Py_hash_t x, y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | if (a->m_self == NULL) |
| 318 | x = 0; |
| 319 | else { |
| 320 | x = PyObject_Hash(a->m_self); |
| 321 | if (x == -1) |
| 322 | return -1; |
| 323 | } |
| 324 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
| 325 | if (y == -1) |
| 326 | return -1; |
| 327 | x ^= y; |
| 328 | if (x == -1) |
| 329 | x = -2; |
| 330 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 334 | PyTypeObject PyCFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 336 | "builtin_function_or_method", |
| 337 | sizeof(PyCFunctionObject), |
| 338 | 0, |
| 339 | (destructor)meth_dealloc, /* tp_dealloc */ |
| 340 | 0, /* tp_print */ |
| 341 | 0, /* tp_getattr */ |
| 342 | 0, /* tp_setattr */ |
| 343 | 0, /* tp_reserved */ |
| 344 | (reprfunc)meth_repr, /* tp_repr */ |
| 345 | 0, /* tp_as_number */ |
| 346 | 0, /* tp_as_sequence */ |
| 347 | 0, /* tp_as_mapping */ |
| 348 | (hashfunc)meth_hash, /* tp_hash */ |
| 349 | PyCFunction_Call, /* tp_call */ |
| 350 | 0, /* tp_str */ |
| 351 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 352 | 0, /* tp_setattro */ |
| 353 | 0, /* tp_as_buffer */ |
| 354 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 355 | 0, /* tp_doc */ |
| 356 | (traverseproc)meth_traverse, /* tp_traverse */ |
| 357 | 0, /* tp_clear */ |
| 358 | meth_richcompare, /* tp_richcompare */ |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame^] | 359 | offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | 0, /* tp_iter */ |
| 361 | 0, /* tp_iternext */ |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 362 | meth_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | meth_members, /* tp_members */ |
| 364 | meth_getsets, /* tp_getset */ |
| 365 | 0, /* tp_base */ |
| 366 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 367 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 369 | /* Clear out the free list */ |
| 370 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 371 | int |
| 372 | PyCFunction_ClearFreeList(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | int freelist_size = numfree; |
| 375 | |
| 376 | while (free_list) { |
| 377 | PyCFunctionObject *v = free_list; |
| 378 | free_list = (PyCFunctionObject *)(v->m_self); |
| 379 | PyObject_GC_Del(v); |
| 380 | numfree--; |
| 381 | } |
| 382 | assert(numfree == 0); |
| 383 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void |
| 387 | PyCFunction_Fini(void) |
| 388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | (void)PyCFunction_ClearFreeList(); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 390 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 391 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 392 | /* Print summary info about the state of the optimized allocator */ |
| 393 | void |
| 394 | _PyCFunction_DebugMallocStats(FILE *out) |
| 395 | { |
| 396 | _PyDebugAllocatorStats(out, |
Antoine Pitrou | 36b045f | 2013-04-11 21:01:40 +0200 | [diff] [blame] | 397 | "free PyCFunctionObject", |
Antoine Pitrou | 0811f98 | 2012-12-30 22:46:04 +0100 | [diff] [blame] | 398 | numfree, sizeof(PyCFunctionObject)); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 399 | } |